WooCommerce - get category for product page

前端 未结 5 1659
野趣味
野趣味 2020-11-30 03:13

For my WC product pages, I need to add a class to the body tag so that I can perform some custom styling. Here\'s the function I\'m creating for this...

func         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 03:37

    A WC product may belong to none, one or more WC categories. Supposing you just want to get one WC category id.

    global $post;
    $terms = get_the_terms( $post->ID, 'product_cat' );
    foreach ($terms as $term) {
        $product_cat_id = $term->term_id;
        break;
    }
    

    Please look into the meta.php file in the "templates/single-product/" folder of the WooCommerce plugin.

    get_categories( ', ', '' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.' ); ?>
    

提交回复
热议问题