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.
<?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
}
}
?>
Stories
is a subcategory ofCharacters
? Hmmm. – Max Yudin Feb 23 '17 at 22:24