0

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?

2
  • What if I want to show child categories though?
    – keg504
    Commented Jul 4, 2020 at 0:40
  • I'll delete that comment, so check my answer instead.
    – Sally CJ
    Commented Jul 4, 2020 at 0:49

1 Answer 1

0

$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:

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' : ''; ?>
4
  • Thanks, your answer made a lot of sense and it's working as I want it to now! I used a slightly different method of checking if the $postCategory != "Leadership", but your answer will be more robust
    – keg504
    Commented Jul 4, 2020 at 0:54
  • Glad it helped, but I added an "Update" there. Check it out, it might help.. :)
    – Sally CJ
    Commented Jul 4, 2020 at 1:19
  • Thanks for the update, I used that. However, I'm still lost about only echoing child categories of the leadership category
    – keg504
    Commented Jul 4, 2020 at 1:43
  • Try wp_list_categories() with the child_of parameter.
    – Sally CJ
    Commented Jul 4, 2020 at 2:37

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.