2016-05-21 15 views
1

私は "brand"という名前の親カテゴリを作成しました。そのカテゴリのIDは "10"です。私は「ナイキ」「アディダスのような子カテゴリを追加して、その中製品アーカイブ内に特定の親カテゴリの子カテゴリを表示しますか?

(」ブランド「など私の製品アーカイブ内

は、私は親カテゴリに関連付けられている子カテゴリ の名前を表示したいです」 ID 10)。例えば

:製品は「ブランド」の子である「ナイキ」(関連付けられている場合 - それは「ナイキ」と表示されますnot-ショー何場合は

私が流れるが、ナッシングを試してみました。私にとっては作品:

<?php 
$categories = get_the_terms(get_the_ID(), '10'); 
if ($categories && ! is_wp_error($category)) : 
    foreach($categories as $category) : 
     $children = get_categories(array ('taxonomy' => '10', 'parent' => $category->term_id)); 

     if (count($children) == 0) { 
      echo $category->name; 
     } 
    endforeach; 
endif; 
?> 

そして:

<?php 
$categories = get_the_terms(get_the_ID(), '10'); 
if ($categories && ! is_wp_error($category)) : 
    foreach($categories as $category) : 
     $children = get_categories(array ('taxonomy' => '10', 'parent' => $category->term_id)); 
     if (count($children) == 0) { ?> 
     <span class="product-sub-cats"><?php echo $category->name; ?></span> 
     <?php } 
    endforeach; 
endif; ?> 

も:

<?php 
if($termid->parent > 10) { 
    $args = array(
     'orderby'  => 'name', 
     'order'   => 'ASC', 
     'hide_empty' => false, 
     'child_of'  => $termid->parent, 
    ); 

$categories = get_the_terms(get_the_ID(), 'product_cat', $args); 
if ($categories && ! is_wp_error($category)) : 
    foreach($categories as $category) : 
     $children = get_categories(array ('taxonomy' => 'product_cat', 'parent' => $category->term_id)); 
     if (count($children) == 0) { ?> 
     <span class="product-sub-cats"><?php echo $category->name; ?></span> 
     <?php } 
    endforeach; 
endif; 
} 
?> 

答えて

0

私はこの問題の解決策を見つけました。

これを達成するためには、あなたが使用する必要があります。 https://codex.wordpress.org/Function_Reference/wp_get_post_terms

これは私のために働いているコードです:

<?php 
global $post; 
$brands_id = get_term_by('slug', 'PARENT-CAT-SLUG', 'product_cat'); 

$terms = get_the_terms($post->ID, 'product_cat'); 
foreach ($terms as $term) { 
    if($term->parent === $brands_id->term_id) { ?> 
     <span class="product-sub-cats"><?php echo $term->name; ?></span> 
     <?php break; 
    } 
} 
?> 
関連する問題