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; ?>
It sounds like get_the_category() would be ideal for this situation, since you're doing this within The Loop:
$post_cats = get_the_category();
if ( $post_cats ) {
foreach ( $post_cats as $cat ) {
// Only show child categories (exclude parents)
if ( ! $cat->category_parent === '0' ) {
echo $cat->cat_name;
}
}
}