2012-04-11 76 views
1

我有一個自定義職位類型,我在其中自定義分類法設置。排除自定義分類的術語?

我想打印出包含帖子但排除帖子的類別(自定義分類)。我找不到排除該類別的解決方案。 這裏是我的代碼輸出的自定義後類型是根據提交的類別列表:

<?php the_terms($post->ID, 'critter_cat', 'Critter Type: ', ', ', ' '); ?> 

如何排除特定類別?

謝謝。

+0

你可以發佈您用於註冊自定義分類代碼? – Robbie 2012-04-11 19:36:20

回答

2

您可以在您的functions.php文件中創建一個函數,該函數調用get_the_terms將術語列表作爲數組返回,然後刪除不想要的項目。

試試這個:

function get_excluded_terms($id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array()) { 
    $terms = get_the_terms($id, $taxonomy); 

    if (is_wp_error($terms)) 
     return $terms; 

    if (empty($terms)) 
     return false; 

    foreach ($terms as $term) { 
     if(!in_array($term->term_id,$exclude)) { 
      $link = get_term_link($term, $taxonomy); 

      if (is_wp_error($link)) 
       return $link; 

      $excluded_terms[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>'; 
      } 
    } 

    $excluded_terms = apply_filters("term_links-$taxonomy", $excluded_terms); 

    return $before . join($sep, $excluded_terms) . $after; 
} 

,然後用它是這樣的:

<?php echo get_excluded_terms($post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ', array(667)); ?> 
+0

我不認爲這是我需要抱歉,也許我不清楚上面?目前,我的帖子顯示他們在類別(自定義分類)A,B,C例如。但我想排除A,因此它只顯示B,C。我希望這是有道理的! – 2012-04-11 18:24:01

+0

所以當你調用'the_terms'時,它會返回'A','B','C'並且你希望它只返回'B','C'? – Robbie 2012-04-11 18:26:24

+0

是的,Robbie就是這樣,謝謝。按ID或名稱排除類別(自定義分類法)的功能。 – 2012-04-11 18:29:51

相關問題