我需要一点帮助,让产品的简短描述显示在产品循环(存档)。我已经将以下代码添加到主题的functions.php文件中:
add_action( 'woocommerce_after_shop_loop_item_title', 'output_product_excerpt', 20 );
function output_product_excerpt() {
global $post;
echo '<div class="my-excerpt">'.wp_trim_words($post->post_excerpt,10).'</div>';
}
它工作得很好,但它适用于所有产品。我可以添加什么来使其仅适用于某个类别?
发布于 2018-06-10 02:41:16
您必须使用get_terms获取该产品的产品类别,将这些类别放入一个数组中,并检查该数组是否包含您的特定类别。
它基本上看起来像这样:
add_action( 'woocommerce_after_shop_loop_item_title', 'output_product_excerpt', 20 );
function output_product_excerpt() {
global $post;
//get the individual products' categories and put them in an array
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) {
$product_categories[] = $term->term_id;
};
//check if the array contains your specific $category_id that you are targeting
if ( is_shop() && in_array( $category_id, $product_categories )) {
echo '<div class="my-excerpt">'.wp_trim_words($post->post_excerpt,10).'</div>';
}
}
https://stackoverflow.com/questions/50776832
复制相似问题