• 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 3

    I 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!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The issue isn’t getting the category in the url, it is determining what the next or previous post in that category even is. If you can determine that, you don’t even need the category in the url. In fact, placing a category will likely cause a 404 because of the way WP parses URLs. It tries to do a category query and a name slug query at the same time. When the two are combined in SQL, nothing comes back.

    To have a category specific post sequence, you basically need a category archive query where there is one page per post and the template displays the entire post. If you want a traditional category archive list as well, you can adapt your single template, but you’ll need custom next/previous link functions that know how to work with your custom category query used for single pages.

Viewing 1 replies (of 1 total)
  • The topic ‘Define the category used in get_next_post’ is closed to new replies.