Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

wordpress

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;?>
like image 998
Nitin Johnson Avatar asked Feb 09 '23 07:02

Nitin Johnson


1 Answers

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;
like image 157
Toretto Avatar answered Feb 12 '23 01:02

Toretto