“Previous Post | Home | Next Post” links in wordpress

Charlene just mentioned this in the comments of another post, so I thought I’d post how I’m doing it on my pages (or will be doing it - once I finish them all)

After the “WordPress Post loop” begins (which starts like this) :

<?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>

Put this:

<?php if ($single) { ?>
<?php previous_post(’% |’,”,’yes’) ?> <a href=”<?php bloginfo(’url’); ?>”>Home</a> <?php next_post(’| %’,”,’yes’) ?>
<?php } ?>

post updated 5/29/04 to include a few of the suggestions in the comments.

30 Responses to ““Previous Post | Home | Next Post” links in wordpress”

  1. KO Says:

    Is there a way to go next and previous in the same category as the post? this method will go to the next post by date - I would like to have it work so it goes to the next post by date in the same category.

  2. kristal Says:

    you are brilliant. thank you!!

  3. Jennifer Says:

    KO - I would think so… I see a plugin in the future ;-) (But I can’t guarantee when…)

  4. Jennifer Says:

    Actually - it looks like you can pass a variable into those functions…

    here’s the function definition:

    previous_post($format=’%', $previous=’previous post: ‘, $title=’yes’, $in_same_cat=’no’, $limitprev=1, $excluded_categories=”)

  5. Jennifer Says:

    See this page for previous post

    and this page for next post

    It’s already in there - no plugin needed. Yee ha! :D

  6. KO Says:

    Thats just brilliant!

    In MT i had to write a bunch of really ugly php code to get this working…

    In WP, will you still need that enter-number-to-post-comment box?

  7. FilSchiesty Says:

    This is a pain. For some reason I cannot implement this code. I dont recieve any errors but my pages dont contain the links.

    After tinkering for a while, I did get the “Home” like to show up, but the prev. next will not.

    I wonder what I’m doing wrong.

  8. Matt Says:

    For the main link you can put <?php bloginfo(’url’); ?>

  9. bran Says:

    Jennifer–thou art the Scripty-est Goddess i know of. thank you for lighting the way toward next/previous link bliss in WP.

  10. Clay Says:

    My code is almost the same, but I didn’t want the pipe (”|”) to show unless there was a link. I didn’t want this: “| Home | Next Article”. I wanted this: “Home | Next Article”. So here’s my code (notice, I don’t have an intro like “Next article”, just the title of the article itself):

    <?php if ($single) { ?>
    <div class=”navigation”><?php previous_post(’% |’,”,’yes’) ?> <a href=”/”>Home</a> <?php next_post(’| %’,”,’yes’) ?></div>
    <?php } ?>

  11. Happy Says:

    Jennifer, thank you for coming up with these great WP tips!

  12. Charlene Says:

    Jennifer, that is too awesome. Thank you. I’m going to play with this soon.

  13. Kathy K Says:

    I wanted a link to next/previous 15 posts on my index page, so I used something exactly opposite this…

    <?php if (!$single) { ?><div class=”pagenav”><?php previous_posts_link(’&laquo; Previous 15 posts ::’) ?> <a href=”index.php”>Main</a> <?php next_posts_link(’:: Next 15 posts &raquo;’) ?> <?php } ?>

    I used ‘if not single’ to keep a lonely ‘Main’ link from showing at the bottom of my individual pages. Thought someone might find the code handy so I posted it here. (You have to set to x “posts paged” in your WP options/Reading for this to work.)

  14. Kathy K Says:

    Oops. There should be an ending </div> right before the <?php } ?>

  15. SilverBlue Says:

    What can I say? You’re the greatest!

  16. Sally Says:

    Is there a way to have wp display “plain” previous/next links, without post titles?

    i.e. you would have: “previous | next” rather than “previous post title | next post title”.

  17. Mark J Says:

    I wanted that on my site too Sally. Here’s what’ll do it:

    <?php if($single){
    previous_post(’%',’« Previous Entry’,'no’,'no’,1,”); ?> |
    <a href=”<?php bloginfo(’url’); ?>”>Main</a> |
    <?php next_post(’%',’Next Entry »’,'no’,'no’,1,”);
    } ?>

  18. Code Novice Says:
    WordPress Bookmarks
    A slew of WP bookmarks, plugins, etc. as seen on Scriptygoddess:

  19. Sally Says:

    Wonderful, Mark — thanks for your help!

  20. identity Says:
    more MT vs. WP
    jill/txt » problems switching from Movable Type to Wordpress My comment is here because I need some of this later, and possibly it might be of use for you too? I find some of your findings (pun not intended) strange….

  21. Greg Says:

    Was anyone able to get the exclude categories function to work with the previous/next links? I’ve tried everything and all posts keep showing up. I basically only want one categories worth of posts to show up in my previous/next links. Ugh.

  22. Sassy Says:

    Excellent! Works great!

  23. Mama Write Says:
    Various WordPress Links
    Rather than ping ScriptyGoddess multiple times and clog my sideblog with even more WordPress links, I decided to plop ‘em all here. I’m going to give WordPress a try again on my course website, since there are various hacks that…

  24. Kara Says:

    Would it be possible to have ‘next’ and ‘previous’ links for categories?

  25. U Rappin' GOO? » WordPress ME¤Ë°Ü¹Ô Says:
    [...] ¬Â¿¤¤¥Ú¡¼¥¸¤òʬ³ä¤·¤ÆÉ½¼¨¥×¥é¥°¥¤¥ó Recently Commented Posts ¡Ä ºÇ¶á¤Î¥³¥á¥ó¥È¥×¥é¥°¥¤¥ó ¡ÖÁ°¤Î¥¨¥ó¥È¥ê¡¼ | ¥È¥Ã¥× | ¼¡¤Î¥¨¥ó¥È¥ê¡¼¡× ¡ [...]

  26. Colorful typhoon » Previous Post | Home | Next Post” links Says:
    [...] ¹ãƒˆãƒŠãƒ“(というのかな?)をつけてみました。参考先はお馴染みのscriptygoddessさんです。各記事のタ゠[...]

  27. One Woman, Five Cats and a Crowbar » More Navigation Than You Can Shake a Stick At! Says:
    [...] to the main page, and b) be able to surf through a series of single posts. First, I found this thread. That let me add previous | main [...]

  28. Tod Says:

    How would I go about making it appear on the index page? I run a daily blog, but would like to use this to add navigation to go to the previous day from the front page. Works great in the archives.
    Thank you.

  29. Tod Says:

    Sorry, wrong plugin. I was referring to your previous/next archive date plugin.

  30. igor Says:

    What is the function: “php next_post”?