Changing to navigation by category in single.php

The default for WordPress is to have the next/previous navigation links go through time not category. You can change this instead to navigation by category in single.php, the file that controls how a post is displayed.

For instance, if you do five different posts in a row

  1. Garlic
  2. Computers
  3. Avacado
  4. Linux
  5. Tomatoes

And you have two categories such as food and computing. When you are on the page about Avacado your next and previous links are to Computers and Linux which doesn’t make much sense if you came to the site in order to look at food.

What would make more sense is to have the next/previous links on the Avacado to be Garlic and Tomatoes.

(Also see how to change where the links are in a post using single.php here)

How to change to navigation by category in single.php

The first step in changing to After changing to navigation by category in single.php is to create a child theme and place a copy of your themes single.php file inside it. I use Twenty Seventeen as my theme so I will be using that code. It is also a good idea to make one change and then test your site so you can easily revert if something goes wrong.

The relevant code begins with the_post_navigation( array( and continues though prev_text and its options and next_text and it’s options until closing with a ) );. I have highlighted that below.

Before changing to navigation by category in single.php

All you have to do is add a 'in_same_term' => 'true', statement before the closing  ) );. I have highlighted that  below. That’s it.

After changing to navigation by category in single.php

Now when you look at the Avacado post the next and previous posts should be through time and category instead of just time.

Leave a Reply

Your email address will not be published. Required fields are marked *