2015-06-26 22 views
1

我正在循環所有帖子,我試圖輸出與每個帖子相關的類別nicename。因此,如果有類別A,B和C,後X僅與類別A和C相關聯,那麼我只想輸出類別A和C的nicename。WordPress的 - 顯示在循環內與帖子ID相關聯的所有子類別

這裏的循環:

<?php $subs = new WP_Query(array('post_type' => 'case-study')); 
    if($subs->have_posts()) : while($subs->have_posts()) : $subs->the_post(); ?> 

    <?php the_title(); ?> 

    <p>Associated Child Categories</p> 
    //Show nicenames of each child category associated to each post 
    <?php $category = get_categories($post->ID); 
     foreach(($category) as $cats) { echo $category->category_nicename; }?> 

<?php endwhile; endif; ?> 

回答

1

這聽起來像get_the_category()將是理想的這種情況,因爲你在循環中這樣做:

$post_cats = get_the_category(); 
if ($post_cats) { 
    foreach ($post_cats as $cat) { 
     // Only show child categories (exclude parents) 
     if (! $cat->category_parent === '0') { 
      echo $cat->cat_name; 
     } 
    } 
} 
+0

這是偉大的,雖然它的輸出父類別以及兒童。我如何排除父母? – egr103

+0

已更新我的回答 – rnevius

+0

很棒,感謝您的額外更新:-) – egr103

相關問題