admin管理员组文章数量:1122832
I am trying to show the attribute of Applications with the word Applications in front of the items that it echos. The code is working for me to echo the items out but I need it so that it only shows the text Applications when there are some with that product.
add_action( 'woocommerce_after_shop_loop_item_title', 'display_applications_attribute', 5 );
function display_applications_attribute() {
global $product;
$taxonomy = 'pa_applications';
echo '<span class="attribute-applications">Applications: ' . $product->get_attribute($taxonomy) . '</span>';
}
I am trying to show the attribute of Applications with the word Applications in front of the items that it echos. The code is working for me to echo the items out but I need it so that it only shows the text Applications when there are some with that product.
add_action( 'woocommerce_after_shop_loop_item_title', 'display_applications_attribute', 5 );
function display_applications_attribute() {
global $product;
$taxonomy = 'pa_applications';
echo '<span class="attribute-applications">Applications: ' . $product->get_attribute($taxonomy) . '</span>';
}
Share
Improve this question
asked Oct 2, 2020 at 21:50
SdesignSdesign
657 bronze badges
1 Answer
Reset to default 0Just make a check, if you have any applications available, and if so - echo them. Here is an example:
add_action( 'woocommerce_after_shop_loop_item_title', 'display_applications_attribute', 5 );
function display_applications_attribute() {
global $product;
$applications = $product->get_attribute('pa_applications');
if ( $applications ) {
printf('<span class="attribute-applications">Applications: %1$s</span>', $applications );
}
}
本文标签: Display WooCommerce product attribute on shop page
版权声明:本文标题:Display WooCommerce product attribute on shop page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736285361a1927443.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论