2017-08-16 22 views
0

現在、このWooCommerceウェブサイトのすべてのカテゴリをリストしたコードスニペットがオンラインで見つかりました。WooCommerceの現在の商品に関連する商品カテゴリのみを表示します

表示している商品に関連するカテゴリを表示するにはどうすればよいですか?

ここで私が微調整したコードです:

<div id="ListCat">     
<h3>Listed in the following categories<?php the_category(); ?></h3> 
<?php 
$taxonomy  = 'product_cat'; 
$orderby  = 'name'; 
$show_count = 0;  // 1 for yes, 0 for no 
$pad_counts = 0;  // 1 for yes, 0 for no 
$hierarchical = 0;  // 1 for yes, 0 for no 
$title  = ''; 
$empty  = 0; 

$args = array(
    'taxonomy'  => $taxonomy, 
    'orderby'  => $orderby, 
    'show_count' => $show_count, 
    'pad_counts' => $pad_counts, 
    'hierarchical' => $hierarchical, 
    'title_li'  => $title, 
    'hide_empty' => $empty 
); 
$all_categories = get_categories($args); 
foreach ($all_categories as $cat) { 
if($cat->category_parent == 0) { 
    $category_id = $cat->term_id;  
    echo ' <a href="'. get_term_link($cat->slug, 'product_cat') 
.'">'. $cat->name .'</a>'; 

    $args2 = array(
     'taxonomy'  => $taxonomy, 
     'child_of'  => 0, 
     'parent'  => $category_id, 
     'orderby'  => $orderby, 
     'show_count' => $show_count, 
     'pad_counts' => $pad_counts, 
     'hierarchical' => $hierarchical, 
     'title_li'  => $title, 
     'hide_empty' => $empty 
    ); 
    $sub_cats = get_categories($args2); 
     if($sub_cats) { 
      foreach($sub_cats as $sub_category) { 
       echo '<br><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>'; 
      } 
     } 
    }  
} 
?> 

+0

あなたが提供したスニペット内で製品を特定する方法を知っていることを尋ねる質問に対する回答を知ることは難しいでしょう。 –

答えて

1

よりはるかに簡単な方法があります:

は、コマ収差が分離され、それを表示するには、文字列(それぞれに製品カテゴリのリンクがあります)

// Display a coma separated string of the product categories for this product 
echo wc_get_product_category_list(get_the_id()); 

(又は完全に類似)

// Display a coma separated string of the product categories for this product 
echo get_the_term_list(get_the_id(), 'product_cat'); 

は、HTML形式のリストとして表示する(それぞれの製品カテゴリへのリンクを有する):

// Display a html formatted list of the product categories for this product 
echo '<ul>' . wc_get_product_category_list(get_the_id(), '</li><li>', '<li>', '</li>') . '</ul>'; 

(またはco mpletely似た)

// Display a html formatted list of the product categories for this product 
echo '<ul>' . get_the_term_list(get_the_id(), 'product_cat', '<li>', '</li><li>', '</li>') . '</ul>'; 

説明:Woocommerce 3では

あなたが見ることができるように、その上に利用できるいくつかの引数を指定して、製品IDから製品カテゴリを一覧表示するwc_get_product_category_list()専用の機能がありますそのソースコード:

/** 
* Returns the product categories in a list. 
* 
* @param int $product_id 
* @param string $sep (default: ', '). 
* @param string $before (default: ''). 
* @param string $after (default: ''). 
* @return string 
*/ 
function wc_get_product_category_list($product_id, $sep = ', ', $before = '', $after = '') { 
    return get_the_term_list($product_id, 'product_cat', $before, $sep, $after); 
} 

このソースコードでわかるように、WordPress get_the_term_list()の別名で、'product_cat'$taxonomyという引数があり、代わりに使用することもできます。

+0

ありがとうございました!私は非常に単純なものを複雑にする傾向があります!コードの最初のスニペットはトリックを行った! –

関連する問題