Home » Wordpress » WordPress next_post_link/previous_post_link not staying in same category

WordPress next_post_link/previous_post_link not staying in same category

Posted by: admin November 7, 2017 Leave a comment

Questions:

Here’s my setup.

single.php

<?php

    if ( in_category( 'my-category' ) ) {
        include( TEMPLATEPATH.'/single-my-category.php' );
    }
    else {
        include( TEMPLATEPATH.'/single-generic.php' );
    }
?>

single-my-category.php

<?php

if ( have_posts() ) : while (have_posts()) : the_post(); ?>

<?php echo the_title(); ?>

<div class="pagination">
    <div class="container">
        <div class="row">
            <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?php next_post_link( '%link', '<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIOUS', true ); ?>
            </div>

            <div class="previous col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?php previous_post_link( '%link', 'NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />', true ); ?>
            </div>
        </div>
    </div>
</div>

<?php endwhile; endif; ?>

This is what i have followed – http://codex.wordpress.org/Function_Reference/next_post_link

I’m not quite sure what I’m doing wrong here as for some reason the previous_post_link is taking me to a post in a different category even though in_same_term parameter of the function is set to true.

Any ideas?

Thanks.

Answers:

Edit your code blocks as following. You haven’t put ‘.php’ on the 5th line of your single.php file. Previous/Next post will be shown only for the posts of category which you specify inside if statement of single.php ( here category ‘php’ ). For the following example, I’ve created a directory ‘template-parts’ and created two php file (“single-my-category.php” and “single-generic.php”)inside that directory.

single.php

<?php
    $the_query = new WP_Query( $post );

    if (in_category('php')) {
        include( 'template-parts/single-my-category.php' );
    } else {
        include( 'template-parts/single-generic.php' );
    }
?>

single-my-category.php

if ( have_posts() ) : while (have_posts() ) : the_post(); ?>

<?php the_title(); ?>

<div class="pagination">
    <div class="container">
        <div class="row">
            <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?php next_post_link( '%link', '<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIOUS', true ); ?>
            </div>

            <div class="previous col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?php previous_post_link( '%link', 'NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />', true ); ?>
            </div>
        </div>
    </div>
</div>

<?php endwhile; endif; ?>

Questions:
Answers:

Also specify the category name in single page .. since single-categoryname.php is not correct you should try using taxonomy-taxonomy_name.php OR

<?php query_posts('category_name=CATEGORYNAME&showposts=5');
while (have_posts()) : the_post();
  // do whatever you want
?>
<?php endwhile;?>
<div class="pagination">
<div class="container">
    <div class="row">
        <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6">
            <?php next_post_link( '%link', '<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIOUS', true ); ?>
        </div>

        <div class="previous col-xs-6 col-sm-6 col-md-6 col-lg-6">
            <?php previous_post_link( '%link', 'NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />', true ); ?>
        </div>
    </div>
</div>

Questions:
Answers:

If in_same_term is not working for you, It might your query object is not holding up the post data.

Give it a try –

global $the_query;
$the_query = new WP_Query( $post );

global $the_query;
if ( $the_query->have_posts() ) : while ($the_query->have_posts() ) : the_post(); ?>

<?php the_title(); ?>

<div class="pagination">
    <div class="container">
        <div class="row">
            <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?php next_post_link( '%link', '<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIOUS', true ); ?>
            </div>

            <div class="previous col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?php previous_post_link( '%link', 'NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />', true ); ?>
            </div>
        </div>
    </div>
</div>

<?php endwhile; endif; ?>

Questions:
Answers:

Can you plz add this code in your template file.

<ul class="pager">
  <?php if ( get_previous_post() != null ) : ?>
    <li class="previous">
      <span class="nav-previous">
        <?php 
          $singular_nav_previous_text = apply_filters( 'tc_singular_nav_previous_text', _x( '&larr;' , 'Previous post link' , 'customizr' ) );
          previous_post_link( '%link' , '<span class="meta-nav">' . $singular_nav_previous_text . '</span> %title' ); 
        ?>
      </span>
    </li>
  <?php endif; ?>
  <?php if ( get_next_post() != null ) : ?>
    <li class="next">
      <span class="nav-next">
        <?php
          $singular_nav_next_text = apply_filters( 'tc_singular_nav_next_text', _x( '&rarr;' , 'Next post link' , 'customizr' ) );
          next_post_link( '%link' , '%title <span class="meta-nav">' . $singular_nav_next_text . '</span>' ); 
          ?>
      </span>
    </li>
  <?php endif; ?>
</ul>

Now add below code in function.php

add_filter('tc_previous_single_post_link_args', 'navigate_in_same_taxonomy');
add_filter('tc_next_single_post_link_args', 'navigate_in_same_taxonomy');
function navigate_in_same_taxonomy( $args ){
  $args['in_same_term'] = true;
  return $args;
}

if you need more option on Next/Prev link check this link