I'm looping through all posts and I'm trying to output the category nicename related to each post. So if there are categories A,B and C with post X only associated with categories A and C then I only want to output category A and C's nicename.

Here's the loop:

<?php $subs = new WP_Query( array( 'post_type' => 'case-study' ));
      if( $subs->have_posts() ) : while( $subs->have_posts() ) : $subs->the_post(); ?>

      <?php the_title(); ?>

      <p>Associated Child Categories</p>
      //Show nicenames of each child category associated to each post
      <?php $category = get_categories($post->ID);
            foreach(($category) as $cats) { echo $category->category_nicename; }?>

<?php endwhile; endif; ?>

1 Answer 1


You should use get_the_categories()

Returns an array of objects, one object for each category assigned to the post. This tag may be used outside The Loop by passing a post id as the parameter.

$categories = get_the_categories();
var_dump( $categories );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.