treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Previous post links

  • Hi All,

    I have a page on a WP site I am doing that contains all posts form the "Blog" Category, but I don't know how to get the previous post link at the bottom of the post.

    I am using this code to display the posts from the category:
    <?php $recent = new WP_Query(); ?>
    <?php $recent->query('cat=3&showposts=4'); ?>
    <?php while($recent->have_posts()) : $recent->the_post(); ?>
    <h2><a href=\"<?php the_permalink() ?>\" rel=\"bookmark\"><?php the_title(); ?></a></h2>
    <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>
    <?php posts_nav_link(); ?>


    You can find the page at http://nimbuswater.com/wordpress/whats-new/

    Thanks for your help!
  • Add this below your code:
    <?php if (  $wp_query->max_num_pages > 1 ) : ?>
    <div id=\"nav-below\" class=\"navigation\">
    <div class=\"nav-previous\"><?php next_posts_link( __( '<span class=\"meta-nav\">&larr;</span> Older posts', 'twentyten' ) ); ?></div>
    <div class=\"nav-next\"><?php previous_posts_link( __( 'Newer posts <span class=\"meta-nav\">&rarr;</span>', 'twentyten' ) ); ?></div>
    </div><!-- #nav-below -->
    <?php endif; ?>

  • Doesn't look like this did anything.
  • If you're on a single post, as in single.php, try this:

    &lt;?php next_post_link('&lt;li class=&quot;prev-icn&quot;&gt;%link&lt;/li&gt;'); ?&gt;
    &lt;?php previous_post_link('&lt;li class=&quot;next-icn&quot;&gt;%link&lt;/li&gt;'); ?&gt;


    If you're on an archive page, a listing of posts, try this:

    &lt;?php next_posts_link('&lt;span class=&quot;prev-icn&quot;&gt;Next&lt;/span&gt;'); ?&gt;
    &lt;?php previous_posts_link('&lt;span class=&quot;next-icn&quot;&gt;Previous&lt;/span&gt;'); ?&gt;


    You can obviously change the argument here to get different results. I'd check the codex for what you can pass to these things.