scriptygoddess RSS Feed
 
 
 
 

"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. 1
    KO:

    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. 2
    kristal:

    you are brilliant. thank you!!

  3. 3
    Jennifer:

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

  4. 4
    Jennifer:

    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. 5
    Jennifer:

    See this page for previous post

    and this page for next post

    It's already in there – no plugin needed. Yee ha! :D

  6. 6
    KO:

    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. 7
    FilSchiesty:

    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. 8
    Matt:

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

  9. 9
    bran:

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

  10. 10
    Clay:

    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. 11
    Happy:

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

  12. 12
    Charlene:

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

  13. 13
    Kathy K:

    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. 14
    Kathy K:

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

  15. 15
    SilverBlue:

    What can I say? You're the greatest!

  16. 16
    Sally:

    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. 17
    Mark J:

    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. 18
    Sally:

    Wonderful, Mark — thanks for your help!

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

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

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

  23. 23
    One Woman, Five Cats and a Crowbar » More Navigation Than You Can Shake a Stick At!:
    [...] 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 [...]

  24. 24
    Greg:

    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.

  25. 25
    Sassy:

    Excellent! Works great!

  26. 26
    Mama Write:
    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…

  27. 27
    Kara:

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

  28. 28
    Tod:

    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. 29
    Tod:

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

  30. 30
    igor:

    What is the function: "php next_post"?

Categories

Archives

Bookmarks

WordPress Resources

Meta

ADVERTISEMENTS