How To Display Previous/Next Post in Category?

By default you see next/previous post link in your theme by publishing date. If you want to display link to previous/next post from same category, you can use following code with in your loop. For eexample you have a category "How to create a WordPress Theme" and you want to display links to something like following You can add following code in your wordpress theme's single.php file.
 
<p>This entry posted in <?php the_category(','); ?> series. Show all in <?php the_category(','); ?>   </p>

<ul class="pager">
  <li class="previous">
    <?php previous_post_link('%link', 'Previous in Series', TRUE); ?>
  </li>
  <li class="next">
    <?php next_post_link('%link', 'Next in Series' , TRUE); ?>
  </li>
</ul> 
 
. I have added above code in my themes single.php file and after adding above code single.php looks like following.
 
  <?php
/**
 * @package TaousDesign
 * @since TaousDesign 1.0
 */
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
 <header class="entry-header">
  <h1 class="entry-title"><?php the_title(); ?></h1>

  <div class="entry-meta">
   <?php taousdesign_posted_on(); ?>

    <?php edit_post_link( __( 'Edit', 'taousdesign' ), '<span class="sep"> | </span><span class="edit-link">', '</span>' ); ?>
  </div><!-- .entry-meta -->
 </header><!-- .entry-header -->
  
  

 <div class="entry-content">

  <p>This entry posted in <?php the_category(','); ?> series. Show all in <?php the_category(','); ?>   </p>

  <ul class="pager">
    <li class="previous">
      <?php previous_post_link('%link', 'Previous in Series', TRUE); ?>
    </li>
    <li class="next">
      <?php next_post_link('%link', 'Next in Series' , TRUE); ?>
    </li>
  </ul> 

  <?php the_content(); ?>

  <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'taousdesign' ), 'after' => '</div>' ) ); ?>
 </div><!-- .entry-content -->

 <footer class="entry-meta">
  <?php
   /* translators: used between list items, there is a space after the comma */
   $category_list = get_the_category_list( __( ', ', 'taousdesign' ) );

   /* translators: used between list items, there is a space after the comma */
   $tag_list = get_the_tag_list( '', ', ' );

   if ( ! taousdesign_categorized_blog() ) {
    
   } else {
    // But this blog has loads of categories so we should probably display them here
    if ( '' != $tag_list ) {
     $meta_text = __( 'Tags :  %2$s.', 'taousdesign' );
    } else {
    }

   } // end check for categories on this blog

   printf(
    $meta_text,
    $category_list,
    $tag_list,
    get_permalink(),
    the_title_attribute( 'echo=0' )
   );
  ?>

  <?php edit_post_link( __( 'Edit', 'taousdesign' ), '<span class="edit-link">', '</span>' ); ?>
 </footer><!-- .entry-meta -->
</article><!-- #post-<?php the_ID(); ?> -->

 
. I hope this will word for you too. Note: i am using twitter bootstrap for styling. .pager is twitter bootstrap class for pagination styling.

0 comments: