2017-12-30 27 views

答えて

0

これはあなたの「親」製品カテゴリのスラッグを設定しますするカスタム関数で可能です:

function get_product_subcategories_list($category_slug){ 
    $terms_html = array(); 
    $taxonomy = 'product_cat'; 
    // Get the product category (parent) WP_Term object 
    $parent = get_term_by('slug', $category_slug, $taxonomy); 
    // Get an array of the subcategories IDs (children IDs) 
    $children_ids = get_term_children($parent->term_id, $taxonomy); 

    // Loop through each children IDs 
    foreach($children_ids as $children_id){ 
     $term = get_term($children_id, $taxonomy); // WP_Term object 
     $term_link = get_term_link($term, $taxonomy); // The term link 
     if (is_wp_error($term_link)) $term_link = ''; 
     // Set in an array the html formated subcategory name/link 
     $terms_html[] = '<a href="' . esc_url($term_link) . '" rel="tag" class="' . $term->slug . '">' . $term->name . '</a>'; 
    } 
    return '<span class="subcategories-' . $category_slug . '">' . implode(', ', $terms_html) . '</span>'; 
} 

コードはアクティブな子テーマ(またはアクティブなテーマ)のfunction.phpファイルに入ります。

テスト済みで動作します。


使用例:

echo get_product_subcategories_list('clothing'); 

あなたでしょう、私の小さな知識

+0

スニペットに感謝します。ただし、現在、サブカテゴリとサブカテゴリの両方が表示されています。私はそれを親カテゴリーの下位サブカテゴリーと一緒にulに入れたいですか? どうすればいいですか? – cvl01

1

これは、特定のカテゴリの取得のサブカテゴリ用のコードです:

$categories = get_the_terms(get_the_ID(), 'product_cat'); 

//For checking category exit or not 

if ($categories && ! is_wp_error($category)) : 


    foreach($categories as $category) : 
     // get the children (if any) of the current cat 
     $children = get_categories(array ('taxonomy' => 'product_cat', 'parent' => $category->term_id)); 

    if (count($children) == 0) { 
     // if no children, then echo the category name. 
     echo $category->name; 
    } 
    endforeach; 

endif; 
+0

申し訳ありませんが、この特定のカテゴリのすべてのサブカテゴリの(リンク付き)水平昏睡区切りのリストを取得するが、どこください私は飼い主のスラグを挿入しますか? – cvl01

関連する問題