wordpress get category name of custom post type in wordpress loop?

I want to get the category name of custom post type in query posts loop.

here is my code:

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;?>

Related posts

1 comment

  1. Try This

    <?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;?>
    

    or

    Try get through terms :

    $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;
    

Comments are closed.