WordPress - 如何在WP_Query中获取类别名称

时间:2015-03-25 14:53:19

标签: wordpress wp-query

我正在查询自定义帖子类型,无法获取帖子所属类别的名称。感觉我已经尝试了所有的东西,但确定它一定是可能的!

                        <?php
global $wpdb;
$currentuser_id = get_current_user_id();
$args = array(
                'post_type' => 'location',
                'author' => $currentuser_id,
);                  
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post(); ?>
        <div class="pindex">
           <?php if ( has_post_thumbnail() ) { ?>
                <div class="pimage">
                    <?php the_post_thumbnail(); ?>
                </div>
            <?php } ?>
            <div class="ptitle">
                <h2><?php echo get_the_title(); ?></h2>
            </div>
                            <div class="pcategory">

                            </div>
        </div>
    <?php endwhile;
    if (  $query->max_num_pages > 1 ) : ?>
        <div id="nav-below" class="navigation">
            <div class="nav-previous"><?php next_posts_link( __( '<span     class="meta-nav">&larr;</span> Previous', 'domain' ) ); ?></div>
            <div class="nav-next"><?php previous_posts_link( __( 'Next     <span class="meta-nav">&rarr;</span>', 'domain' ) ); ?></div>
        </div>
    <?php endif;
endif;
wp_reset_postdata();
?>

有人知道下面的代码来获取类别名称吗?

<div class="pcategory">
</div>

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

<?php
$terms = get_the_terms( $post->ID , 'gd_placecategory' );
foreach ( $terms as $term ) {
      echo $term->name;
}
?>

由于需要自定义帖子类型get_the_terms()

请注意,您将自定义帖子类型传递到WP_Query - &#39;位置&#39;在这种情况下,您需要get_the_terms()&#39; gd_placecategory&#39;

中的自定义分类