I have some posts assigned to more than one category. The loop I'm using is only for specific categories. But I need to display the assigned category name. When there's a post that fits into my loop because it's assigned one of the categories I'm using, but it is also assigned to one that's not, I need to only display the category name for the loop I'm currently using.
So, let's say I have a post assigned to "properties", and "featured". I only want to show posts assigned to "properties" (and several others). I have this code:
$args = array(
'post_status' => 'publish',
'cat' => array(32,31,34,33),
'orderby' => 'date',
'order' => 'DESC',
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$cats = get_the_category();
$cat_slug = '';
if ( ! empty( $cats ) ) {
foreach ( $cats as $cat ) {
$cat_slug .= $cat->slug . ' ';
}
}
echo $cat_slug;
}
}
It shows all category names
And when I use
the_category(', ');
It shows all categories and links (which I would expect for that one).
I need to NOT display the category name that's not part of the current loop.