wordpress 如何在Woocommerce中单个产品页面上的类别链接前添加额外的文本?

6l7fqoea  于 2023-03-17  发布在  WordPress
关注(0)|答案(3)|浏览(153)

我想补充“类别:“文本之前的类别链接(在同一行)在Woocommerce单一产品页面。目前用户(特别是老用户,我的目标是我的页面)可能会混淆什么是链接。下面的例子(黄色标记):

当前代码如下所示,位于“摘要条目-摘要”div中:

<span class="single-product-category">
    <a href="https://www.store.domain/category-name/" rel="tag">bielizna ochronna</a>
</span>

不编辑模板是否可行?

myzjeezk

myzjeezk1#

好吧,我想我可能会分享我的“变通办法”(因为我不喜欢通过CSS添加内容到页面);然而仍然指望一些聪明的想法如何做不同的;- )

.single-product-category:before {
  content: "Kategoria: ";
}
gcuhipw9

gcuhipw92#

functions.php添加底层代码

add_filter( 'woocommerce_product_single_category_title', 'add_text_before_category_link', 10, 2 );

function add_text_before_category_link( $category_title, $category ) {
    // Replace "Extra Text" with the text you want to add before the category link
    return 'Extra Text '. $category_title;
}

保存functions.php文件中的更改。刷新单个产品页面以查看更新后的类别链接。

m3eecexj

m3eecexj3#

add_action( 'woocommerce_single_product_summary', 'add_category_text', 1 );
function add_category_text() {
global $product;
$terms = get_the_terms( $product->get_id(), 'product_cat' );
if ( ! empty( $terms ) ) {
    $term = array_pop( $terms );
    echo '<p>' . __('Category: ', 'woocommerce') . '<a href="' . get_term_link( $term ) . '">' . $term->name . '</a></p>';
  }
}

相关问题