Wordpress 是否从自定义分类中排除术语?

Wordpress 是否从自定义分类中排除术语?,wordpress,taxonomy,custom-post-type,Wordpress,Taxonomy,Custom Post Type,我有一个自定义的文章类型,其中我有自定义的分类设置 我想打印出帖子包含的类别(自定义分类法),但不包括其中一个。但我无法找到排除该类别的解决方案。 以下是我的代码,用于输出自定义帖子类型所属类别的列表: <?php the_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ' ); ?> 如何排除特定类别 谢谢。您可以在functions.php文件中创建一个函数,调用get\u the\u terms以

我有一个自定义的文章类型,其中我有自定义的分类设置

我想打印出帖子包含的类别(自定义分类法),但不包括其中一个。但我无法找到排除该类别的解决方案。 以下是我的代码,用于输出自定义帖子类型所属类别的列表:

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

如何排除特定类别


谢谢。

您可以在functions.php文件中创建一个函数,调用
get\u the\u 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;
}
函数get_excluded_terms($id=0,$taxonomy,$before='',$sep='',$after='',$exclude=array()){
$terms=获取术语($id,$taxonomy);
如果(是错误($terms))
返回$terms;
如果(空($条款))
返回false;
foreach($terms作为$term){
if(!in_数组($term->term_id,$exclude)){
$link=get\u term\u link($term$taxonomy);
如果(是错误($link))
返回$link;
$excluded_条款[]='';
}
}
$excluded_terms=应用_过滤器(“term_链接-$taxonomy”,$excluded_terms);
返回$before.join($sep,$excluded\u terms)。$after;
}
然后像这样使用它:

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


我不认为这是我所需要的对不起,也许我上面不清楚?目前,我的帖子显示它们属于类别(自定义分类法),例如A、B、C。但我想排除A,所以它只显示B,C。我希望这是有意义的!因此,当您调用
术语时,它返回
A
B
C
,您希望它只返回
B
C
?是的,Robbie,谢谢。通过ID或名称排除类别(自定义分类法)的功能。是的,我也这么认为。。。这就是它的作用。。。嗯,我不能测试,所以我希望可以。它怎么不适合你?注意,请参阅您的页面,我已经更新了几次答案;)我已经复制并粘贴了,但什么也没有出现。我已将ID添加到第一行。我是否需要编辑此行:“未设置($terms,$except);//您可以发布用于注册自定义分类的代码吗?