当前帖子的ID排除条款(类别名称)

时间:2014-11-21 14:03:06

标签: php wordpress categories taxonomy term

我发现此功能显示附加到帖子的条款,但我无法找到一种方法来排除我不想在列表中显示的某些类别条款的特定ID。 有人能给我一个线索吗?我查找了此函数中使用的所有函数,但似乎无法找到排除id的参数。

提前致谢!

   // get taxonomies terms links
   function custom_taxonomies_terms_links() {
   global $post, $post_id;
   // get post by post id
   $post = &get_post($post->ID);
   // get post type by post
   $post_type = $post->post_type;
   // get post type taxonomies
   $taxonomies = get_object_taxonomies($post_type);
   $out = "<ul>";
   foreach ($taxonomies as $taxonomy) {        
       // get the terms related to post
       $terms = get_the_terms( $post->ID, $taxonomy );
       if ( !empty( $terms ) ) {
           foreach ( $terms as $term )
               $out .= '<li><a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a></li> ';
        }
    }
    $out .= "</ul>";
    return $out;
} 

1 个答案:

答案 0 :(得分:2)

在第二个if中添加另一个条件foreach()语句,以检查是否应忽略$term。例如:

// An array of IDs to ignore/exclude
$excluded_ids = array( 1, 2, 3, 4);

foreach ( $terms as $term ) {
    // Only proceed if the term_id is NOT in the $excluded_ids array
    if ( !in_array( $term->term_id, $excluded_ids ) ) {
        $out .= '<li><a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a></li> ';
    }
}