2016-07-05 277 views
2

我有一個自定義帖子,我想顯示它的一部分的所有類別。有兩個部分這一點,在頁面我只顯示了頂級類別,它的一部分的頂部,這是我做的:只顯示帖子的子類別

<?php $term_list = wp_get_post_terms($post->ID, 'project-categories', array('fields' => 'all', 'parent' => 0)); 
foreach($term_list as $term) { ?> 

    <div class="type-block"> 
     <span class="type-initial"><?php echo the_field('category_initials', 'project-categories_' . $term->term_id); ?></span> 
     <span class="type-name type-name-<?php echo $term->term_id; ?>"><?php echo $term->name; ?></span> 
    </div>        

<?php } ?> 

走向我想在頁面的底部只顯示它所屬的子類別 - 這些子類別可能是幾個頂級類別的一部分。我如何顯示它們?這是我的嘗試:

<?php $term_list = wp_get_post_terms($post->ID, 'project-categories', array('fields' => 'all', 'parent' => array(7, 10, 8, 9, 11))); 
foreach($term_list as $term) { ?> 

    <div class="type-block type-block-black"> 
     <span class="type-initial"><?php echo the_field('category_initials', 'project-categories_' . $term->term_id); ?></span> 
     <span class="type-name type-name-<?php echo $term->term_id; ?>"><?php echo $term->name; ?></span> 
    </div>        

<?php } ?> 

目前它只顯示最後一個子類別,如何顯示多個類別?

回答

2

試試這個代碼

<?php 
    $term_list = wp_get_post_terms($post->ID, 'project-categories', array('fields' => 'all')); 
    $term_list = wp_list_filter($term_list, array('parent'=>'0'),'NOT'); 
    foreach($term_list as $term) { ?> 

     <div class="type-block type-block-black"> 
      <span class="type-initial"><?php echo the_field('category_initials', 'project-categories_' . $term->term_id); ?></span> 
      <span class="type-name type-name-<?php echo $term->term_id; ?>"><?php echo $term->name; ?></span> 
     </div>        

<?php } ?> 
+0

非常巧妙,非常感謝! – Rob

相關問題