简体   繁体   English

如何在Wordpress中的产品详细信息页面上获得产品的类别名称

[英]How to get the Category name of product on product detail page in wordpress

I am new in wordpress and I am using RT-Theme 17. I am developing e-commerce site and I want product category name when I go to the specifically a product detail. 我是wordpress的新手,正在使用RT-Theme17。我正在开发电子商务网站,当我要转到具体的产品详细信息时,我想要产品类别名称。 how to show the product category name there. 如何在此处显示产品类别名称。

I also used this source code but I did not get category id(term_id) 我也使用了此源代码,但没有获得类别ID(term_id)

$cate = get_queried_object();     
$cateID = $cate->term_id;     
echo $cateID;

I found the result is:- 我发现结果是:-

WP_Post Object
(
    [ID] => 1140
    [post_author] => 1
    [post_date] => 2016-07-27 13:42:38
    [post_date_gmt] => 2016-07-27 13:42:38
    [post_content] => Energizer professional series heavy duty jumper cables for all vehicle types plus full size trucks, vans and SUVs. Red/black PVC-coated insulated clamps for easy identification. Tangle-free cables remains flexible even at -40°C. Heavy duty copper. 1 Gauge, 30 Ft, 800 AMP. All weather use.
    [post_title] => ENB130
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] => 
    [post_name] => enb130
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2016-08-03 19:05:34
    [post_modified_gmt] => 2016-08-03 19:05:34
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => http://energizerpower.com/?post_type=products&p=1140
    [menu_order] => 0
    [post_type] => products
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
)

You can try below code. 您可以尝试以下代码。 It display all categories (separate by comma) assigned to that product . 它显示分配给该产品的所有类别(用逗号分隔)。

<?php global $post, $product;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM