Php WooCommerce-获取产品页面的类别

Php WooCommerce-获取产品页面的类别,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,对于我的WC产品页面,我需要向body标记添加一个类,以便执行一些自定义样式。这是我为这个创建的函数 function my_add_woo_cat_class($classes) { $wooCatIdForThisProduct = "?????"; //help! // add 'class-name' to the $classes array $classes[] = 'my-woo-cat-id-' . $wooCatIdForThisProduct;

对于我的WC产品页面,我需要向body标记添加一个类,以便执行一些自定义样式。这是我为这个创建的函数

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 cat ID?

WC产品可能不属于任何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;
}
请查看WooCommerce插件“templates/single product/”文件夹中的meta.php文件

<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>

感谢盒。我正在使用MyStile主题,我需要在搜索结果页面中显示产品类别名称。我将此函数添加到我的子主题函数.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
    }
}


?>
/*Post-Meta*/
如果(!function_存在('woo_post_meta')){
函数woo_post_meta(){
全球$woo_期权;
全球$员额;
$terms=get_the_terms($post->ID,'product_cat');
foreach($terms作为$term){
$product\U cat=$term->name;
打破
}
?>

  • 我从主题目录中woocommerce文件夹中的content-single-popup.php中逐行删除了这行代码

    global $product; 
    echo $product->get_categories( ', ', ' ' . _n( ' ', '  ', $cat_count, 'woocommerce' ) . ' ', ' ' );
    

    由于我正在处理的主题已将woocommerce集成到其中,因此这是我的解决方案。

    $product->get_categories()
    自3.0版以来已被弃用!请改用
    wc_get_product_categories_list


    要向body标记添加自定义类,可以使用钩子

    要根据特定产品类别在产品页面上添加一个或多个类,可以使用Wordpress功能(检查产品是否属于该特定产品类别)

    为此,我创建了数组
    $classes\u to\u add
    ,其中:

    • :它可以是产品类别id(或slug或名称),也可以是它们的数组
    • value:包含要添加到body标记的类的字符串。如果要添加多个类,请创建一个字符串,其中包含多个由空格分隔的值(请参见下面的示例)
    因此:


    代码已经过测试,可以正常工作。将其添加到活动主题的functions.php中。

    刚刚将此代码添加到“price.php”中,当访问单个产品页面时,我收到以下错误消息:警告:为foreach()提供的参数无效。我是否做错了什么,或者这段代码是否与当前的WC版本不兼容?有没有办法用它排除某些类别?这帮助我获得了产品slug,并在保存新评论后使用它重定向到类别列表。这是我的问题,如果有任何帮助。这是令人惊讶的。我甚至为多个类别设置了cats数组s、 非常好的回答!谢谢,它返回类别名称,如何获取类别id,而不是其名称?请提供准确的描述,说明这有助于回答OP的问题。请阅读-描述您的代码。这与公认的答案非常相似,只是它回显类别名称,而不是返回类别id!@ban geoengineer(好的)
    <?php
       $terms = get_the_terms($product->ID, 'product_cat');
          foreach ($terms as $term) {
    
            $product_cat = $term->name;
               echo $product_cat;
                 break;
      }
     ?>
    
    // adds a class to the body element based on the product category
    add_filter( 'body_class', 'add_body_class_based_on_the_product_category' );
    function add_body_class_based_on_the_product_category( $classes ) {
    
        // only on the product page
        if ( ! is_product() ) {
            return $classes;
        }
    
        // create an array with the ids (or slugs) of the product categories and the respective class (or classes) to add
        $classes_to_add = array(
            30          => 'class-1',
            'cat_slug'  => 'class-2 class-3',
            32          => 'class-4 class-5',
        );
    
        // if the product belongs to one of the product categories in the array it adds the respective class (or classes)
        foreach ( $classes_to_add as $product_cat => $new_classes ) {
            if ( has_term( $product_cat, 'product_cat', get_the_ID() ) ) {
                 $classes[] = $new_classes;
            }
        }    
    
        return $classes;
    }