Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress From Bootstrap to WordPress Create Bootstrap Styled Theme Templates Creating a Portfolio Landing Page

the_category(' '); will not work on WP_Query

trying to figure out why when I do

$the_query = new WP_Query( $args );

and use

<p>Categories: <?php the_category(' '); ?></p>

in the loop it will not display the categories. Why is that?

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Matthew,

It's hard to tell based on your code but it appears you're not calling the method the_post on the post item which is what sets up the globals for the core WordPress functions such as the_title, the_category etc. You would essentially have the following:

<?php

$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) {
  $the_query->the_post();
  <p>Categories: <?php the_category(' '); ?></p>
}

// It's always best to reset the original post data
wp_reset_postdata();

Ill post the complete code tonight when I have it in front of me but what I am trying to do is echo out all the categories as text and not links.