仅显示帖子的子类别

时间:2016-07-05 10:10:06

标签: php wordpress

我有一个自定义帖子,我想展示它所属的所有类别。这里有两个部分,在页面顶部我只显示了它所属的顶级类别,这就是我做的方式:

<?php $term_list = wp_get_post_terms($post->ID, 'project-categories', array('fields' => 'all', 'parent' => 0));
foreach($term_list as $term) { ?>

    <div class="type-block">
        <span class="type-initial"><?php echo the_field('category_initials', 'project-categories_' . $term->term_id); ?></span>
        <span class="type-name type-name-<?php echo $term->term_id; ?>"><?php echo $term->name; ?></span>
    </div>                              

<?php } ?>

在页面底部,我只想显示它所属的子类别 - 这些子类别可能是几个顶级类别的一部分。我该如何显示它们?这是我的尝试:

<?php $term_list = wp_get_post_terms($post->ID, 'project-categories', array('fields' => 'all', 'parent' => array(7, 10, 8, 9, 11)));
foreach($term_list as $term) { ?>

    <div class="type-block type-block-black">
        <span class="type-initial"><?php echo the_field('category_initials', 'project-categories_' . $term->term_id); ?></span>
        <span class="type-name type-name-<?php echo $term->term_id; ?>"><?php echo $term->name; ?></span>
    </div>                              

<?php } ?>

目前它只显示最后一个子类别,如何显示多个类别?

1 个答案:

答案 0 :(得分:2)

试试此代码

<?php 
    $term_list = wp_get_post_terms($post->ID, 'project-categories', array('fields' => 'all'));
    $term_list = wp_list_filter($term_list, array('parent'=>'0'),'NOT');
    foreach($term_list as $term) { ?>

        <div class="type-block type-block-black">
            <span class="type-initial"><?php echo the_field('category_initials', 'project-categories_' . $term->term_id); ?></span>
            <span class="type-name type-name-<?php echo $term->term_id; ?>"><?php echo $term->name; ?></span>
        </div>                              

<?php } ?>
相关问题