I want my single.php to display posts in the same category for the previous and next posts underneath the post. The problem is that each of my posts belongs to multiple categories, and they are displayed through one of the other categories (24) versus the one I want them to display from (27). Does that even make sense?

Example Categories:

Characters (parent category) (Subcategory IDs listed below:)

  • 24 (This category ID displaying instead.)
  • 27 (This is the category ID that I want to display.)

Now, my question is, how do I choose the category I want to be pulled from (27) instead of the one being automatically pulled (24)? Here is my code below (that I've found and been fiddling with), with what I've tried so far.

    if (is_single() && in_category('stories')) {
        $post_id = $post->ID;
        $cat = get_the_category(); //I've tried changing this to my category (both ID and slug)
        $current_cat_id = $cat[0]->cat_ID;  //Also tried plugging ID and slug

        $args = array(
            'category' => $current_cat_id, //Also tried plugging ID and slug
            'orderby' => 'post_date',
            'order' => 'DESC'
        $posts = get_posts($args);

        $ids = array();
        foreach ($posts as $thepost) {
            $ids[] = $thepost->ID;

        $thisindex = array_search($post_id, $ids);
        $previd = $ids[$thisindex - 1];
        $nextid = $ids[$thisindex + 1];

        if (!empty($nextid)) {
            ?><div class="double-grid"><a rel="next" href="<?php echo get_permalink($nextid) ?>"><div class="image-tile tile-on-archive-page" style="background-image: url('<?php echo get_the_post_thumbnail_url($nextid); ?>'"> <div class="gold-button">LAST STORY >></div></div></a></div><?php
        if (!empty($previd)) {
            ?><div class="double-grid"><a rel="prev" href="<?php echo get_permalink($previd) ?>"><div class="image-tile tile-on-archive-page" style="background-image: url('<?php echo get_the_post_thumbnail_url($previd); ?>'"> <div class="gold-button">NEXT STORY >></div></div></a></div><?php
  • It is not clear where are categories and subcategories.
    – Max Yudin
    Commented Feb 23, 2017 at 20:51
  • How so? What more do you need to know about the categories?
    – finamf92
    Commented Feb 23, 2017 at 22:08
  • So, Stories is a subcategory of Characters? Hmmm.
    – Max Yudin
    Commented Feb 23, 2017 at 22:24
  • That is correct. "Characters" is the category, and "Stories" is the subcategory that I want to be displayed. However, another subcategory "John" is being displayed instead. I want to know how to tell the code to select "Stories" instead.
    – finamf92
    Commented Feb 23, 2017 at 22:28
  • Especially since my posts are under multiple categories and subcategories.
    – finamf92
    Commented Feb 23, 2017 at 22:33

2 Answers 2


Your code problem is you get only the first category of a post because $cat[0] is the first, not current.

Inside The Loop:

previous_post_link( // prints the formatted link
    '<span>&laquo; %link</span>',  // beautifying around
    'Prev in this cat',            // the text (%link) to be used in the span above
    true,                          // confirm that you want only this category
    '',                            // no categories to exclude
    'category'                     // taxonomy name

echo " | "; // just a separator between next and prev

/* The same setup here, not shown to keep the answer clean */

See previous_post_link() for parameters.

If you are outside of The Loop, let me know, I'll update the answer and supply the additional code and comments.

  • I have tried this. The problem with this one is when I have multiple categories assigned to the post. Then it selects the first category ID that is assigned to the post--rather than the specific category I want.
    – finamf92
    Commented Feb 24, 2017 at 16:23
  • On another note: I've tried excluding the categories that I don't want to show up (as shown on the Codex page you included), however, it doesn't seem to work. It still picks the category with the lowest ID that is on the post to navigate between. I need to be able to pick the one I want it to read.
    – finamf92
    Commented Feb 24, 2017 at 16:36
  • Don't be nervous. I'll get it done because it's the interesting situation for me too. Just keep cool.
    – Max Yudin
    Commented Feb 24, 2017 at 17:04
  • Print the echo $cat[0]->cat_ID; on every page you are dreaming. What it say?
    – Max Yudin
    Commented Feb 24, 2017 at 17:20
  • It prints the number 24 when I inspect the page. (Which is the ID for my "John" category, rather than the 27 of my "Stories" category.)
    – finamf92
    Commented Feb 24, 2017 at 17:27

Hazah! I thought I'd post it here, but I worked with someone on the development forums on Wordpress.org and they actually solved the problem for me.

You can see the solution and tweaks here: WordPress.org Forum Topic

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.