如何根据产品类别在 woocommerce 产品页面上添加 link?
How to add a link on woocommerce product page as per the product category?
我能够在 woocommerce 产品页面上找到添加 link 的代码,但我想根据产品类别添加 link。以下是我添加的用于在产品页面上显示 link 的代码。有人可以帮我获取代码以根据产品类别将 link 添加到产品页面吗?
add_action( 'woocommerce_before_add_to_cart_button',
'content_before_addtocart_button' );
function content_before_addtocart_button() {
echo '<div class="content-section">add custom size <a
href="http://google.com">here</a></div>';
}
将此代码放在您的 functions.php 文件中,如果在这种情况下类别 slug 匹配 'jackets',它将输出 link。
add_action( 'woocommerce_before_add_to_cart_button', 'content_before_addtocart_button' );
function content_before_addtocart_button() {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
if( $term->slug === 'jackets')
echo '<div class="content-section">add custom size <a href="' . esc_url( get_term_link( $term->term_id, 'product_cat' ) ) . '">' . $term->name . '</a></div>';
}
}
我能够在 woocommerce 产品页面上找到添加 link 的代码,但我想根据产品类别添加 link。以下是我添加的用于在产品页面上显示 link 的代码。有人可以帮我获取代码以根据产品类别将 link 添加到产品页面吗?
add_action( 'woocommerce_before_add_to_cart_button',
'content_before_addtocart_button' );
function content_before_addtocart_button() {
echo '<div class="content-section">add custom size <a
href="http://google.com">here</a></div>';
}
将此代码放在您的 functions.php 文件中,如果在这种情况下类别 slug 匹配 'jackets',它将输出 link。
add_action( 'woocommerce_before_add_to_cart_button', 'content_before_addtocart_button' );
function content_before_addtocart_button() {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
if( $term->slug === 'jackets')
echo '<div class="content-section">add custom size <a href="' . esc_url( get_term_link( $term->term_id, 'product_cat' ) ) . '">' . $term->name . '</a></div>';
}
}