Previous/next post links displaying wrong category than is desired
-
Okay, I’ve combed the internet, raked the Codex, fiddled with code, and am still having one issue with my code. It–sort of–works. But not quite how I want it to. (It’s all local development at the moment, so I can’t link, but I’ll try to do a good job at describing.)
What I want to accomplish is have my single.php display posts in the same category for the previous and next posts underneath the post. The problem is that each of my posts are in multiple categories, and they are display through one of the other categories versus the one I want them to display from. Does that even make sense?
Example Categories:
Characters (parent category) (Subcategories listed below:)
– Tony
– John (This one is displaying instead.)
– Farah
– Stories (I want these ones to display.)Now, my question is, how do I choose the category I want to be pulled from instead of the one being automatically pulled? Here is my code below (that I’ve found and been fiddling with), with what I’ve tried so far.
<?php 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 } } ?>
Any input appreciated!
- The topic ‘Previous/next post links displaying wrong category than is desired’ is closed to new replies.