【问题标题】:Display the category names for the current post, but exclude one ID显示当前帖子的类别名称,但不包括一个 ID
【发布时间】:2021-10-08 16:24:34
【问题描述】:

我试图显示自定义帖子类型分类的当前帖子类别名称,但排除其中一个类别。我在 StackOverflow 中看到了不同的解决方案,但我似乎无法让它们中的任何一个为我工作。下面是我正在使用的代码,效果很好,但我不知道如何使用它来排除一个 ID。

<?php $terms = get_the_terms( $post->ID , 'press_category' ); 
                    foreach ( $terms as $term ) {
                        $term_link = get_term_link( $term, 'press_category' );
                        if( is_wp_error( $term_link ) )
                        continue;
                    echo  $term->name ;
                    } 
                ?>

【问题讨论】:

    标签: wordpress custom-taxonomy


    【解决方案1】:

    试试这个

    <?php $terms = get_the_terms($post->ID, 'press_category');
    foreach ($terms as $term)
    {
        $term_link = get_term_link($term, 'press_category');
        if (is_wp_error($term_link)) continue;
        if ($term->term_id != 222)
        {
            echo $term->name;
        }
    }
    ?>
    

    将此行中的 222 更改为您的分类 ID 以排除

    $term->term_id != 222

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      • 2021-11-28
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多