如何在wordpress的产品详细信息页面上获取产品的类别名称

时间:2016-08-08 09:59:09

标签: php wordpress woocommerce wordpress-theming

我是wordpress的新手,我正在使用RT-Theme 17.我正在开发电子商务网站,当我专门针对产品细节时,我想要产品类别名称。如何在那里显示产品类别名称。

我也使用了这个源代码,但我没有得到类别id(term_id)

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

我发现结果是: -

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
)

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码。它显示分配给该产品的所有类别(以逗号分隔)。

<?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>' ); ?>