首页 > 解决方案 > Wordpress - 在单页上显示类别

问题描述

我使用 ACF 创建自定义帖子类型。对于这种自定义类型,我有两种不同的分类法:

在房屋类型中,我有 4 个不同的类别:

每个帖子都需要“所有类型”和另一个(例如:所有类型,房屋)

我想显示主要类别:房屋而不显示:所有类型我如何隐藏“所有类型”?

我的代码:

  <div class="detailfiche">
                                <?php 
                                    global $post;
                                    $terms = wp_get_post_terms($post->ID, 'housetype');
                                    if ($terms) {
                                        $output = array();
                                        foreach ($terms as $term) {
                                            $output[] = '<span><a href="' .get_term_link( $term->slug, 'housetype') .'">' .$term->name .'</a></span>';
                                        }
                                        echo join( ' ', $output );
                                    };
                                    ?>
                            </div>

标签: wordpressadvanced-custom-fields

解决方案


试试这个

<?php
global $post;
$terms = wp_get_post_terms($post->ID, 'housetype');
if ($terms)
{
    $output = array();
    foreach ($terms as $term)
    {
        if ($term->term_id != 222)
        {
            $output[] = '<span><a href="' . get_term_link($term->slug, 'housetype') . '">' . $term->name . '</a></span>';
        }

    }
    echo join(' ', $output);
};
?>

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

$term->term_id != 222


推荐阅读