按当前帖子的 ID 排除术语(类别名称)


Excluding terms (category names) by ID for current post

我发现这个函数可以显示附加到帖子的术语,但我无法找到一种方法来排除我不想在列表中显示的某些类别术语的特定 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;
} 

在第二个foreach()中添加另一个条件if语句,以检查是否应忽略$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> ';
    }
}