我为有价值的产品创建了一个名为 "代金券 "的分类法:1代金券,2代金券,3代金券,。
我想在存档页面上打印出每个产品的分类值,像这样。
我想使用一个函数(在我的function.php中)来插入这个值(分类学)。
function display_taxonomy_product_archives() {
echo "VALUE OF THE COUCHAGE TAXONOMY";
}
add_action( 'woocommerce_after_shop_loop_item_title', 'display_taxonomy_product_archives', 25 );
希望能得到帮助。
在下面的代码中设置你的 自定义分类法 以在每个产品的档案页上显示术语名称。
1)在 "加入购物车 "前的 "查看更多 "按钮。
add_action( 'woocommerce_after_shop_loop_item_title', 'display_taxonomy_product_archives', 25 );
function display_taxonomy_product_archives() {
global $product;
// HERE below define your custom taxonomy
$taxonomy = 'product_cat';
$terms = wp_get_post_terms( $product->get_id(), $taxonomy, ['fields' => 'names']);
if( ! empty($terms) ) {
// Display the term names
echo '<p class="' . $taxonomy . '">' . implode(', ', $terms) . '</p>';
}
}
2) 在 "添加到购物车""查看更多 "按钮之后。
add_action( 'woocommerce_after_shop_loop_item', 'display_taxonomy_product_archives', 30 );
function display_taxonomy_product_archives() {
global $product;
// HERE below define your custom taxonomy
$taxonomy = 'product_cat';
$terms = wp_get_post_terms( $product->get_id(), $taxonomy, ['fields' => 'names']);
if( ! empty($terms) ) {
// Display the term names
echo '<p class="' . $taxonomy . '" style="margin:10px 0 0">' . implode(', ', $terms) . '</p>';
}
}
代码放在你的活动子主题(或活动主题)的function.php文件中。经过测试,可以使用。
文档化的Wordpress wp_get_post_terms()
功能...