Define the category used in get_next_post
-
Hi,
I have posts that have different categories. Some of them share same categories in addition of having different ones.
Example:
Post 1 has Categoy 1 and Category 2
Post 2 has Category 2 and Category 3
Post 3 has Category 1 and Category 3I need to be able to choose which category is going to be used in next /previous posts.
Example:
For Post 1 use Category 1 only. (which will reach post 3 as next post, not Post 2).I checked the function reference in WordPress codex and it can’t be done with the parameters of /get next post. You can only exclude certain categories, use the current category or specify a taxonomy.
I need to be able to specify the category of the next / previous post by myself.My idea is to get the category current printed in the url.
I’m using the following code, where:
$categoryslug is the category slug printed in the url that I want to use to tell WordPress which category will be used in next / previous posts.
Code:<?php $url = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?> <?php if (false !== strpos($url,'?c')) : ?> <div id="nav-portfolio"> <?php $categoryurl = explode('/', $url); $categoryslug = str_replace("?c=","",$curl); ?> <div class="prev"> <?php $prev_post = get_previous_post(); if (!empty( $prev_post )): ?> <a href="<?php echo get_permalink( $prev_post->ID ); ?><?php echo $categoryurl[sizeof($categoryurl)-2];?>/"><?php echo $prev_post->post_title; ?></a> <?php endif; ?> </div> <div class="next"> <?php $next_post = get_next_post(); if (!empty( $next_post )): ?> <a href="<?php echo get_permalink( $next_post->ID ); ?> <?php echo $categoryurl[sizeof($categoryurl)-2];?>/"><?php echo $next_post->post_title; ?></a> <?php endif; ?> </div> </div> <?php endif; ?>
Getting the category slug from the URL works well, but I need a way to tell WordPress to use only this category. Maybe working over this part?
$next_post = get_next_post();
Any idea of how to call only a specific category in nex / prev post function?
You can check the website I’m working on here:
https://magoz.is/_beta/follow-your-dreams/?c=personal/Thank you very much in advance folks!
- The topic ‘Define the category used in get_next_post’ is closed to new replies.