Php wordpress是否在wordpress循环中获取自定义帖子类型的类别名称?
我想在查询帖子循环中获取自定义帖子类型的类别名称 这是我的密码:Php wordpress是否在wordpress循环中获取自定义帖子类型的类别名称?,php,wordpress,Php,Wordpress,我想在查询帖子循环中获取自定义帖子类型的类别名称 这是我的密码: 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="<?
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;?>
query\u posts(数组('post\u type'=>'portfolio','posts\u per\u page'=>4,'orderby'=>'ID','order'=>'DESC');
while(have_posts()):the_post();
试试这个
<?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;?>
在循环中尝试此操作:
@IndrasinhBihola它仍然不起作用,尽管我在循环中获得id,但仍然没有获得类别名称尝试此操作:它仍然不起作用我尝试像这样打印代码,并且我得到一个空白数组$category=get_the_category($post->id);印刷品(类别)@NitinJohnson您是否能够通过$post->id
获取帖子id,并且yopu是否为帖子选择了多个类别?是的,我正在获取帖子id。否,我只为自定义帖子分配了一个类别type@NitinJohnson我通过使用术语添加了另一个解决方案,看看它是否有效?使用术语的代码非常有效。很好的解决方案。
$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;