我想在查询帖子循环中获取自定义帖子类型的类别名称。
这是我的代码:
query_posts(array('post_type'=>'portfolio','posts_per_page'=>4, 'orderby' => 'ID', 'order' => 'DESC' ));
while ( have_posts() ) : the_post();
<li>
<div class="foli_img"> <a href="<?php echo get_permalink();?>"> <span class="next"> </span> </a> <?php the_post_thumbnail();?> </div>
<h3 class="style"><?php the_title();?></h3>
<?php the_content();?>
<h4><a href="#">I want the category name here</a></h4>
</li>
<?php endwhile;?>
答案 0 :(得分:5)
试试这个
<?php
while ( have_posts() ) :
the_post();?>
<li>
<div class="foli_img">
<a href="<?php echo get_permalink();?>">
<span class="next"> </span>
</a>
<?php the_post_thumbnail();?>
</div>
<h3 class="style"><?php the_title();?></h3>
<?php the_content();?>
<h4><a href="#">
<?php
$category = get_the_category( $post->ID );
echo $category[0]->cat_name;?></a></h4>
</li>
<?php
endwhile;?>
或
尝试通过条款:
$terms = get_the_terms($post->ID, 'Enter_your_taxonomy_here' );
if ($terms && ! is_wp_error($terms)) :
$tslugs_arr = array();
foreach ($terms as $term) {
$tslugs_arr[] = $term->slug;
}
$terms_slug_str = join( " ", $tslugs_arr);
endif;
echo $terms_slug_str;