获取自定义帖子类型的所有类别 [英] Get all categories of a custom post type

查看:59
本文介绍了获取自定义帖子类型的所有类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取自定义帖子类型的所有类别。我正在使用 get_the_category(); 函数检索类别。但是如果我有3个帖子,其中1个category类别重复了3次:o。

I am trying to get all categories of a custom post type.I am using get_the_category(); function to retrieve the categories.But if i have 3 posts with 1 category the category is repeated 3 times :o .

<?php 
    query_posts( array( 'post_type' => 'member', 'showposts' => 8 ) );
    if ( have_posts() ) : while ( have_posts() ) : the_post();
        $categories = get_the_category();
            foreach ( $categories as $category ) { 
        echo $category->name, ' '; 
    }             
?>
<?php endwhile; endif; wp_reset_query(); ?>

有没有解决方案?

推荐答案

,您可以在此处获取get_terms的参考

<?php
$taxonomy = 'YOUR TEXONOMY NAME';
$terms = get_terms($taxonomy);

if ( $terms && !is_wp_error( $terms ) ) :
?>
    <ul>
        <?php foreach ( $terms as $term ) { ?>
            <li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
        <?php } ?>
    </ul>
<?php endif;?>

这篇关于获取自定义帖子类型的所有类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆