$leaderQuery->the_post()
should only be called once, right after the loop starts:
while ( $leaderQuery->have_posts() ) {
$leaderQuery->the_post(); // call it just once
...
}
I could also see you’re not correctly retrieving the ID of the current post in the loop, so the correct way is use get_the_ID()
.
$postCategories = get_the_category( get_the_ID() ); // like this
$postCategories = get_the_category($leaderQuery->the_post()); // not this
But within a loop, you can simply call get_the_category()
like that, i.e. without any parameters.
And note that get_the_category()
returns an array of term objects, so if you just want to get the name of the first category in that array, then you can do:
// Get all categories (term objects) assigned to the current post.
$postCategories = get_the_category();
// Get the first category's name.
$first_cat_name = ( ! empty( $postCategories ) ) ? $postCategories[0]->name : '';
But if you want to display all the categories, then you can simply use the_category()
.
And note that for custom taxonomies, you would:
-
Use
get_the_terms()
and notget_the_category()
. -
Use
the_terms()
and notthe_category()
.
Update
Actually, in your WP query’s args, that cat_slug
is not a valid parameter for WP_Query
. Instead, it should be category_name
, but remember that it expects a slug and not the category name, e.g. my-category
and not My Category
.
Also, you could use something like so to display a specific category name only if the current post is in that category:
if ( in_category( 'Leadership' ) ) {
echo 'In the Leadership category';
}
// Or after closing the PHP tag ( ?> ):
//<?php echo in_category( 'Leadership' ) ? 'In the Leadership category' : ''; ?>