Search code examples
phpwordpresscustom-taxonomy

WordPress: Styling taxonomy slug page


I have a custom taxonomy called Topics.

Topics currently has three categories:

enter image description here

Note the count of posts for each category above.

When a user goes to a topic page, i.e. /topics/news, I want to show all posts related to news neatly, so looking to write custom markup.

To do this, I have come across taxonomy templates, but getting weird results.

For starters, I'm on /topics/news. From the above image, you can see News has 2 posts.

Here is my taxonomy-topics.php file:

<?php get_header();

  if ( have_posts() ){
    while ( have_posts() ) {
      the_title();
    }
  }

get_footer(); ?>

Just trying to show the title of the news posts at the moment. However, it is looping through and printing the title for one post several times. Seems like there's an infinite loop happening.


Solution

  • You must call the_post() so that the post index is moved to the next one in the posts array in the main query (i.e. the $wp_query global):

    while ( have_posts() ) {
        the_post(); // call this or you'll be stuck in an infinite loop! :)
        the_title();
    }