Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress next_post_link/previous_post_link not staying in same category

Tags:

php

wordpress

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.

like image 346
Andrew Matthew Avatar asked Mar 16 '16 11:03

Andrew Matthew


2 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; ?>
like image 107
Sajib Biswas Avatar answered Sep 23 '22 04:09

Sajib Biswas


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>

like image 35
Prakash Rao Avatar answered Sep 22 '22 04:09

Prakash Rao