Title: Next and Previous Posts
Last modified: August 19, 2016

---

# Next and Previous Posts

 *  Resolved [ccradmin](https://wordpress.org/support/users/ccradmin/)
 * (@ccradmin)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/)
 * I have a blog and i want my template to only show one post(done) and displays
   links(done) to the next and previous posts.
 * Also how would I check for oldest and newest.
 * So to be clear here is my questions:
 * 1) How do I get the links for the next and previous posts of the current post?
   
   2) how do i check for the oldest and newest post?

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/next-and-previous-posts-1/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/next-and-previous-posts-1/page/2/?output_format=md)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749355)
 * Not quite sure what the proper answer should be.
 * 1) If you are wanting to paginate after a query_posts loop, you must include 
   the ‘paged=’ argument in the call to query_posts and then use the previous/next_posts_link()
   functions to get the links.
 * 2) Do you want to display the posts, or just control the previous/next links?
   If you want to display the posts, you will need queries to search for the max
   or min of post_date. If you want to control the pagination, that is built in 
   to previous/next_posts_link().
 *  Thread Starter [ccradmin](https://wordpress.org/support/users/ccradmin/)
 * (@ccradmin)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749383)
 * When you click the previous or next link is displays the next or previous post
   in place of the current one
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749384)
 * That is exactly what my answer to 1) above is intended to do.
 * > 1) If you are wanting to paginate after a query_posts loop, you must include
   > the ‘paged=’ argument in the call to query_posts and then use the previous/
   > next_posts_link() functions to get the links.
 * I can’t be more specific without seeing the code in your template. If you are
   using a free theme, post a link to a page that shows the problem so I can take
   a look.
 * If you are using a paid theme, contact the theme supplier.
 *  Thread Starter [ccradmin](https://wordpress.org/support/users/ccradmin/)
 * (@ccradmin)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749390)
 * is this what im looking for?
 *     ```
       <?php
       				$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       				$args=array(
          								'cat'=>3,
          								'caller_get_posts'=>1,
          								'paged'=>$paged,
          							);
       				query_posts($args);
       			?>
       ```
   
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749392)
 * That looks correct to me. Is it not working?
 * You didn’t show the code for the previous/next_posts_link calls, so the problem
   might not be obvious.
 *  Thread Starter [ccradmin](https://wordpress.org/support/users/ccradmin/)
 * (@ccradmin)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749398)
 * Heres all the code for the post
 *     ```
       <?php if ( have_posts() ) : the_post(); ?>
   
                    <!-- The following tests if the current post is in category 3. -->
                    <!-- If it is, the div box is given the CSS class "post-cat-three". -->
                    <!-- Otherwise, the div box will be given the CSS class "post". -->
                    <?php if ( in_category('3') ) { ?>
                              <div class="post-cat-three">
                    <?php } else { ?>
                              <div class="post">
                    <?php } ?>
   
                    <?php
       				$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       				$sticky=get_option('sticky_posts');
       				$args=array(
          								'cat'=>3,
          								'caller_get_posts'=>1,
          								'post__not_in' => $sticky,
          								'paged'=>$paged,
          							);
       				query_posts($args);
       ?>
   
                    <!-- Display the Title as a link to the Post's permalink. -->
                    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/latestnews.png" style="margin-top:5px; margin-left:0;"/></a></h2>
                    <small>Author: <?php the_author_posts_link(); ?></small>
   
                    <!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
                    <div class="datebg">
       			 	<span class="date">
       					<?php the_time('F j, Y') ?>
                       </span>
                    </div>
   
                    <!-- Display the Post's Content in a div box. -->
                    <div class="entry">
               	 <?php 
   
       				$entry = the_content();
       				$limit = 200;
       			 	substr($entry,0,$limit);
       			 ?>
                    	<table class="postnav" style="margin-top:5px;">
                       	<tr>
                           	<td><a href="<?php get_next_post() ?>"><img src="<?php bloginfo('template_url'); ?>/images/newernone.png" class="newer" /></a></td>
                               <td><a href="#"><img src="<?php bloginfo('template_url'); ?>/images/readmore.png" class="readmore" /></a></td>
                           	<td><a href="<?php get_adjacent_post(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/older.png" class="older" /></a></td>
                           </tr>
                      </table>
   
                    </div>
   
                    </div> <!-- closes the first div box -->
   
                    <!-- Stop The Loop (but note the "else:" - see next line). -->
                    <?php else: ?>
   
                    <!-- The very first "if" tested to see if there were any Posts to -->
                    <!-- display.  This "else" part tells what do if there weren't any. -->
                    <p>Sorry, no posts matched your criteria.</p>
   
                    <!-- REALLY stop The Loop. -->
                    <?php endif; ?>
       ```
   
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749402)
 *     ```
       <table class="postnav" style="margin-top:5px;">
                       	<tr>
                           	<td><a href="<?php get_next_post() ?>"><img src="<?php bloginfo('template_url'); ?>/images/newernone.png" class="newer" /></a></td>
                               <td><a href="#"><img src="<?php bloginfo('template_url'); ?>/images/readmore.png" class="readmore" /></a></td>
                           	<td><a href="<?php get_adjacent_post(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/older.png" class="older" /></a></td>
                           </tr>
                      </table>
       ```
   
 * there are a few things not working in your code:
 * `get_next_post()` does **not **output the url of the next post;
    `get_adjacent_post()`
   does also **not **output the url of the adjacent post.
 * see:
    [http://codex.wordpress.org/Function_Reference/get_next_post](http://codex.wordpress.org/Function_Reference/get_next_post)
   [http://codex.wordpress.org/Function_Reference/get_adjacent_post](http://codex.wordpress.org/Function_Reference/get_adjacent_post)
 * to get these links, you would probably use:
 * `next_posts_link()` and `previous_posts_link()`
    [http://codex.wordpress.org/Function_Reference/next_posts_link](http://codex.wordpress.org/Function_Reference/next_posts_link)
   [http://codex.wordpress.org/Function_Reference/previous_posts_link](http://codex.wordpress.org/Function_Reference/previous_posts_link)
 * the check, if there are next or prev posts, is best done with a conditonal statement
   using ‘get_next_posts_link()’
 * a possible way of re-writing your code:
    [http://wordpress.pastebin.com/mi8pMStz](http://wordpress.pastebin.com/mi8pMStz)
 * **before editing your theme files, make a backup copy**
 *  Thread Starter [ccradmin](https://wordpress.org/support/users/ccradmin/)
 * (@ccradmin)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749403)
 * its not working, both next and previous buttons are faded out even though i have
   5 posts made
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749404)
 * re-work the structure of your loop:
 * [http://codex.wordpress.org/The_Loop_in_Action](http://codex.wordpress.org/The_Loop_in_Action)
 * imho, your code is missing a `while(have_posts())` and `query_posts()` is coming
   too late in the code; it should be either before or just after the `if(have_posts())`
 *  Thread Starter [ccradmin](https://wordpress.org/support/users/ccradmin/)
 * (@ccradmin)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749405)
 * but isnt that going to make it display more than one post at a time?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749406)
 * query_posts()
 * posts_per_page
 * [http://codex.wordpress.org/Function_Reference/query_posts#Post_.26_Page_Parameters](http://codex.wordpress.org/Function_Reference/query_posts#Post_.26_Page_Parameters)
 *  Thread Starter [ccradmin](https://wordpress.org/support/users/ccradmin/)
 * (@ccradmin)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749407)
 * This is what i have now but the buttons still dont work.
 *     ```
       <?php if ( have_posts() ) : the_post(); ?>
                    <?php while (have_posts()) : the_post(); ?>
                    <?php
       				$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       				$sticky=get_option('sticky_posts');
       				$args=array(
          								'cat'=>3,
          								'caller_get_posts'=>1,
          								'post__not_in' => $sticky,
          								'paged'=>$paged,
       								'posts_per_page' => 1
          							);
       				query_posts($args);
       ?>
   
                    <!-- The following tests if the current post is in category 3. -->
                    <!-- If it is, the div box is given the CSS class "post-cat-three". -->
                    <!-- Otherwise, the div box will be given the CSS class "post". -->
                    <?php if ( in_category('3') ) { ?>
                              <div class="post-cat-three">
                    <?php } else { ?>
                              <div class="post">
                    <?php } ?>
   
                    <!-- Display the Title as a link to the Post's permalink. -->
                    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/latestnews.png" style="margin-top:5px; margin-left:0;"/></a></h2>
                    <small>Author: <?php the_author_posts_link(); ?></small>
   
                    <!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
                    <div class="datebg">
       			 	<span class="date">
       					<?php the_time('F j, Y') ?>
                       </span>
                    </div>
   
                    <!-- Display the Post's Content in a div box. -->
                    <div class="entry">
               	 <?php 
   
       				$entry = the_content();
       				$limit = 200;
       			 	substr($entry,0,$limit);
       			 ?>
   
                      <table class="postnav" style="margin-top:5px;">
                       	<tr>
       <?php if(get_next_posts_link()) { $next_posts_img = '<img src="' . get_bloginfo('template_url') . '/images/newer.png" class="newer" />'; ?>
       <td><?php next_posts_link($next_posts_img); ?></td>
       <?php  }
       else { ?><td><img src="<?php bloginfo('template_url'); ?>/images/newernone.png" class="newer" /></td>
       <?php } ?>
       <td><a href="#"><img src="<?php bloginfo('template_url'); ?>/images/readmore.png" class="readmore" /></a></td>
       <?php if(get_previous_posts_link()) { $prev_posts_img = '<img src="' . get_bloginfo('template_url') . '/images/older.png" class="older" />'; ?>
       <td><?php previous_posts_link($prev_posts_img); ?></td>
       <?php  }
       else { ?><td><img src="<?php bloginfo('template_url'); ?>/images/oldernone.png" class="older" /></td>
       <?php } ?>
       </tr>
       </table>
   
                    </div>
   
                    </div> <!-- closes the first div box -->
   
                    <!-- Stop The Loop (but note the "else:" - see next line). -->
                    <?php endwhile; ?>
                    <?php else: ?>
   
                    <!-- The very first "if" tested to see if there were any Posts to -->
                    <!-- display.  This "else" part tells what do if there weren't any. -->
                    <p>Sorry, no posts matched your criteria.</p>
   
                    <!-- REALLY stop The Loop. -->
   
                    <?php endif; ?>
       ```
   
 *  Thread Starter [ccradmin](https://wordpress.org/support/users/ccradmin/)
 * (@ccradmin)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749416)
 * I need a fix soon
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749417)
 * We need a link to the blog page on your site. And, you need to move the query_posts
   call to the top, like this:
 *     ```
       <?php
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       $sticky=get_option('sticky_posts');
       $args=array(
          'cat'=>3,
          'caller_get_posts'=>1,
          'post__not_in' => $sticky,
          'paged'=>$paged,
          'posts_per_page' => 1
       );
       query_posts($args);
       ?>
       <?php if ( have_posts() ) : the_post(); ?>
                    <?php while (have_posts()) : the_post(); ?>
       ```
   
 *  Thread Starter [ccradmin](https://wordpress.org/support/users/ccradmin/)
 * (@ccradmin)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/#post-1749420)
 * I have it all working the way I want it to. Thanks for all the help

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/next-and-previous-posts-1/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/next-and-previous-posts-1/page/2/?output_format=md)

The topic ‘Next and Previous Posts’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 16 replies
 * 3 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [15 years, 7 months ago](https://wordpress.org/support/topic/next-and-previous-posts-1/page/2/#post-1749422)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
