对于我的WC产品页面,我需要一个类添加到身体标记,这样我可以进行一些定制的造型。 下面是我创建这个功能...
function my_add_woo_cat_class($classes) {
$wooCatIdForThisProduct = "?????"; //help!
// add 'class-name' to the $classes array
$classes[] = 'my-woo-cat-id-' . $wooCatIdForThisProduct;
// return the $classes array
return $classes;
}
//If we're showing a WC product page
if (is_product()) {
// Add specific CSS class by filter
add_filter('body_class','my_add_woo_cat_class');
}
...但我怎么得到WooCommerce猫ID?
一个WC产品可能属于无,一个或多个类别的WC。 假如你只是想获得一个WC类别ID。
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
请看meta.php文件中的“模板/单品/”的WooCommerce插件的文件夹中。
<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
我简直出条纹从位于woocommerce文件夹在我的主题目录中的内容,单popup.php这行代码。
global $product;
echo $product->get_categories( ', ', ' ' . _n( ' ', ' ', $cat_count, 'woocommerce' ) . ' ', ' ' );
由于我的主题,我的工作已经整合woocommerce在里面,这是我的解决方案。
$product->get_categories()
自3.0版本已过时! 使用wc_get_product_category_list
代替。
https://docs.woocommerce.com/wc-apidocs/function-wc_get_product_category_list.html
由于箱。 我使用MyStile主题,我需要在我的搜索结果页面中显示的产品类别名称。 我添加了这个功能,我的子主题的functions.php
希望它可以帮助别人。
/* Post Meta */
if (!function_exists( 'woo_post_meta')) {
function woo_post_meta( ) {
global $woo_options;
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
break;
}
?>
<aside class="post-meta">
<ul>
<li class="post-category">
<?php the_category( ', ', $post->ID) ?>
<?php echo $product_cat; ?>
</li>
<?php the_tags( '<li class="tags">', ', ', '</li>' ); ?>
<?php if ( isset( $woo_options['woo_post_content'] ) && $woo_options['woo_post_content'] == 'excerpt' ) { ?>
<li class="comments"><?php comments_popup_link( __( 'Leave a comment', 'woothemes' ), __( '1 Comment', 'woothemes' ), __( '% Comments', 'woothemes' ) ); ?></li>
<?php } ?>
<?php edit_post_link( __( 'Edit', 'woothemes' ), '<li class="edit">', '</li>' ); ?>
</ul>
</aside>
<?php
}
}
?>