WordPress php Only show next post link in same category

Im using the next post link function of WP to show next and previous navigation links to the “next post in same category” all good with this…the issue i have is that when I’m in the “last” post of a category, the “NEXT” link appears anyway and doing what i dont want: linking to a different category 1st post. the same with the 1st post of a category, when browsing the first post of a category the “PREVIOUS” link appears linking to previous post of a another different category… I would like that in the 1st and last post, this links aren’t shown.

   <div id="nav-above" class="navigation">
                        <div class="nav-previous"><?php previous_post_linknext_post_link( '%link', '<span class="meta-nav">' .     
        _x( '◄ Previous', 'Previous post link','category' ,TRUE ) . '</span>' ); ?></div>
                        <div class="nav-next"><?php next_post_link( '%link', '<span class="meta-nav">' . _x( 'Next ► ', 'Next post link', 'category',TRUE ) . '</span>' ); ?> </div>
                    </div><!-- #nav-above -->

Related posts

Leave a Reply

1 comment

  1. You need to test whether there is a next or previous post by using the get_previous_post_link and get_next_post_link functions. For example:

    <div id="nav-above" class="navigation">
    
        <?php $prev = get_previous_post_link( '%link', '<span class="meta-nav">' .     
        _x( '&#9668; Previous', 'Previous post link','category' ,TRUE ) . '</span>' ); ?>
        <?php if ($prev) : ?>
        <div class="nav-previous"><?php echo $prev ?></div>
        <? endif; ?>
    
        <?php $next = get_next_post_link( '%link', '<span class="meta-nav">' . _x( 'Next &#9658; ', 'Next post link', 'category',TRUE ) . '</span>' ); ?>
        <?php if ($next) : ?>
         <div class="nav-next"><?php echo $next ?> </div>
        <?php endif; ?>
    
    </div><!-- #nav-above -->