簡體   English   中英

如何在Wordpress中的產品詳細信息頁面上獲得產品的類別名稱

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

我是wordpress的新手,正在使用RT-Theme17。我正在開發電子商務網站,當我要轉到具體的產品詳細信息時,我想要產品類別名稱。 如何在此處顯示產品類別名稱。

我也使用了此源代碼,但沒有獲得類別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
)

您可以嘗試以下代碼。 它顯示分配給該產品的所有類別(用逗號分隔)。

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