【问题标题】:Excluding terms (category names) by ID for current post当前帖子的按 ID 排除术语(类别名称)
【发布时间】:2014-11-21 14:03:06
【问题描述】:

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

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    在第二个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> ';
        }
    }
    

    【讨论】:

    • 您好,感谢您的快速回复。但是当我输入不同的 id 时,我不想看到它最终只显示我想要排除的类别名称的名称
    • 这不应该发生。听起来您在in_array( 之前缺少!
    • 好吧抱歉我做错了:)。它完美无缺!非常感谢!!
    猜你喜欢
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 2012-08-30
    • 1970-01-01
    相关资源
    最近更新 更多