Wordpress-获取自定义文章类型的类别名称



我的代码返回默认帖子类型的类别名称"帖子":

<div class="row masonary-wrap">
<?php 
$args = array(
'post_type' => 'project',
'post_status' => 'publish',
'posts_per_page' => '6'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$categories = get_categories( $args ); 
foreach ( $categories as $cat ) : ?>
<div class="col-lg-4 col-md-6 col-12 port-item mb-30 <?php echo esc_attr( $cat->cat_name ); ?>">    
<div class="project">
<div class="proj-img">
<div class="proj-overlay">
<h5><?php the_title(); ?></h5>                          
</div>
</div>
</div>
</div>
<?php
endforeach;
endwhile;
wp_reset_postdata();
endif; ?>
</div>

我需要显示类别名称或自定义帖子类型的更好的片段";项目";,不是默认帖子!

您可以这样做。请检查下面的代码

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

谢谢,

最新更新