I have a website for an organisation, and I created a new page for the leadership team. I ran a query, looking for the "member"-type that have the category of "Leadership". So far, when I output this on the page, it works fine. I also have child categories of "President" and "Secretary" that I want to display beside each leader's name. However, as soon as I call get_the_category()
, everything gets messed up, and only one result is output, and the President category somehow disappears and the post with the category of "President" gets "Secretary" displayed next to it.
This is the relevant code:
<?php
$leaderQuery = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'member',
'cat_slug' => "Leadership"
));
//var_dump($leaderQuery);
while($leaderQuery->have_posts())
{
$postCategories = get_the_category($leaderQuery->the_post());
//$categoryDescription = category_description($postCategories[0]);
//var_dump($postCategories);
$leaderQuery->the_post(); ?>
<div class="post-item">
<p class="headline headline--small"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> - <?php echo get_the_category($leaderQuery->the_post())->name; ?></p>
</div>
<?php
}
wp_reset_postdata();
echo paginate_links();
?>
var_dump($leaderQuery->the_post())
does not contain category information, but var_dump($postCategories)
does. This is the var_dump($postCategories)
:
array (size=2)
0 =>
object(WP_Term)[1446]
public 'term_id' => int 7
public 'name' => string 'Joint Secretary' (length=15)
public 'slug' => string 'joint-secretary' (length=15)
public 'term_group' => int 0
public 'term_taxonomy_id' => int 7
public 'taxonomy' => string 'category' (length=8)
public 'description' => string 'B' (length=1)
public 'parent' => int 6
public 'count' => int 1
public 'filter' => string 'raw' (length=3)
public 'cat_ID' => int 7
public 'category_count' => int 1
public 'category_description' => string 'B' (length=1)
public 'cat_name' => string 'Joint Secretary' (length=15)
public 'category_nicename' => string 'joint-secretary' (length=15)
public 'category_parent' => int 6
1 =>
object(WP_Term)[1441]
public 'term_id' => int 6
public 'name' => string 'Leadership' (length=10)
public 'slug' => string 'leadership' (length=10)
public 'term_group' => int 0
public 'term_taxonomy_id' => int 6
public 'taxonomy' => string 'category' (length=8)
public 'description' => string '' (length=0)
public 'parent' => int 0
public 'count' => int 2
public 'filter' => string 'raw' (length=3)
public 'cat_ID' => int 6
public 'category_count' => int 2
public 'category_description' => string '' (length=0)
public 'cat_name' => string 'Leadership' (length=10)
public 'category_nicename' => string 'leadership' (length=10)
public 'category_parent' => int 0
This is the wrong data for the current post that is actually output on the page, and of the two, only one is displayed, and the other gives its category information.
How do I make it so each post correctly retrieves the category name and displays it alongside the post title?