请问wordpress中如何获取自定义post_type的分类

我再wordpress 自定义的post type中想要获取当前文章的分类。分类也是自己用register_taxonomy来自定义的。

<div class="cell-home">
<?php query_posts(array( 'post_type' => 'wcp_employees' ));?>
  <?php if ( have_posts() ) : ?>
   <?php query_posts(array( 'post_type' => 'wcp_employees' )); ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <dl>
            <dt style="background-image:url(<?php bloginfo('stylesheet_directory'); ?>/images/employees/01.jpg);">
                <div class="title"><?php the_category(', ');  ?></div>
            </dt>
            <dd style="display:block;">
                <div class="txt-content">
                    <h3><?php the_title(); ?></h3>
                    <p><img class="thumb" src="<?php bloginfo('stylesheet_directory'); ?>/images/employees/04.jpg"></p>
                    <p><?=get_post_excerpt()?></p>
                    <p class="tr"><a href="<?php the_permalink(); ?>" class="btn-more">More</a></p>
                </div>
            </dd>
        </dl>
    <?php ;endwhile; twentyfourteen_paging_nav();?>
    <?php else : ?>
    <?php endif; ?>
</div>

这里我也用了query_posts 来指定自己的分类,但是分类还是现实不出来。

阅读 7.3k
2 个回答

找到方法了,用这个function

function custom_taxonomies_terms_links(){
    //根据当前文章ID获取文章信息
    $post = get_post( $post->ID );

    //获取当前文章的文章类型
    $post_type = $post->post_type;

    //获取文章所在的自定义分类法
    $taxonomies = get_object_taxonomies( $post_type, 'objects' );

    $out = array();
    foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
        $term_list = wp_get_post_terms($post->ID, $taxonomy_slug, array("fields" => "all"));
        echo $term_list[0]->name; //显示文章所处的分类中的第一个
    }

    return implode('', $out );
}
新手上路,请多包涵
/**
 *获取分类
 **/
function deel_category(){
    $tag_arr = get_the_category();
    foreach($tag_arr as $value){
        if(!empty($value)){
            echo '<a data-bear="main" href='.get_term_link($value->slug,$value->taxonomy).'>'.$value->name.'<a>';
        }
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题