Home » Blog Optimization, Blog Post, How-to, PHP tutorial

How to Display (Navigation) Links to Previous and Next Posts on your Blog Entry (WordPress)

19 April 2008 42 Comments

Many WordPress themes by default do not have the previous post and next post buttons on the single post page, that is when you open a particular post from the main WordPress blog. If I like a particular post that I come across, its very likely that I’ll dig through the rest of the posts on that blog. And for that, I don’t like to load the blog-home-page each time after reading a blog entry.So, previous and next post navigation-buttons are quite an indispensible features of a blog that I love to see on whatever blog I read.

A template file of a WordPress blog is basically a PHP file and the way your single post page displays is determined by a file called single.php which is present in your \wp-content\themes\yourtheme\ directory. So, you get an idea that for adding the previous and next post links (or buttons, whatever you choose) , we’ll have to edit this file called single.php

Now let’s see how we do it. Take a look at how a most basic single.php file can look like

<!–single.php–>

<!–This is the loop that checks if there are any posts to display. If there’s no post a custom error message is displayed.Else the requested post shows up!–>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<!–This is the navigation that we intend to add to a blog entry. As this is placed above the title of my post (see the next piece of code) , the previous and next buttons appear above the title of my blog post–>
<p><?php previous_post_link(‘« %link |’) ?> <a href=”<?php bloginfo(‘url’); ?>”>Home</a> <?php next_post_link(‘| %link »’) ?></p>

<!–This shows the post title–>
<h1 id=”post-<?php the_ID(); ?>”><a href=”<?php echo get_permalink() ?>” rel=”bookmark” title=”Permanent Link: <?php the_title(); ?>”><?php the_title(); ?></a></h1>

<!–This displays the author name below the title of the post–>
<p><b>By <?php the_author(); ?></b> | <?php the_time(‘F j, Y’); ?></p>

<!–This part shows the main blog entry–>
<div class=”postspace2″>
</div>
<!–content with more link–>
<?php the_content(‘<p class=”serif”>Read the rest of this entry »</p>’); ?>
<!–for paginate posts–>

<!–This part is the footer of the blog-post that shows the categories and comments–>
<?php link_pages(‘<p><strong>Pages:</strong> ‘, ‘</p>’, ‘number’); ?>
<p><b>Topics:</b> <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’, ”, ‘ | ‘); ?> <?php comments_popup_link(‘No Comments »’, ’1 Comment »’, ‘% Comments »’); ?></p>

<div class=”postspace”>
</div>
<!–almost all options over and out–>

The text inside <!– –> in the code are comments that only describe what a piece of code does. I have elaborated them for your convenience, here. They may not be exactly the same in your template.

So what you basically need to add navigation (like on my blog’s single blog entry) is this piece of code:

<p><?php previous_post_link(‘« %link |’) ?> <a href=”<?php bloginfo(‘url’); ?>”>Home</a> <?php next_post_link(‘| %link »’) ?></p>

<?php previous_post_link(‘« %link |’) ?> : This part shows the name of the previous post, hyperlinked to it. The | character after %link shows as | to separate this link from the rest of the links we are going to add. That is link to the home page and to the next post.

<a href=”<?php bloginfo(‘url’); ?>”>Home</a> : This part shows the text Home linked to the home page

<?php next_post_link(‘| %link »’) ?> : This part shows the name of the next post, hyperlinked to it after the | symbol.

This is a simple and very useful navigational aid. But thats not all about it. Only a few people know that this is a good way to improve your internal linking for SEO purposes. Your post will always link to two posts and the homepage with the very appropriate anchor text, that is the title itself.

So, if you don’t have this feature on your blog’s single blog entry page, go ahead, add it as shown above and reap the benefits ;) I hope this PHP tutorial helps you.

Subscribe to Inspirit’s RSS feed for receiving any future updates right in your e-mail.

42 Comments »

  • Alex at Net-Entrepreneur.com said:

    Very good..I think mainly for SEO, because few readers click on those.

    Cheers,
    Alex.

    Alex at Net-Entrepreneur.com’s last blog post..Increasing Blog Stickiness

  • RT Cunningham said:

    Thanks for this and for being “do follow”. :-)

    RT Cunninghams last blog post..Building Backlinks with Blogs that Do Follow, not No Follow

  • Geoserv said:

    Perfect, thanks a lot.
    VOTED for you at NewsDot

    Geoservs last blog post..Trackback?

  • Howie Black Hat Reviews said:

    Hey look at me! I’m spamming your blog with my comment! Can you do the same? – Howie

  • Prestamos Personales said:

    Now that\’s something to think about. Glad I stopped by – keep up the good work.Ciao!

  • Free Wii said:

    Cheers for the info, will come in handy with my new website project

  • Rowing Machines said:

    Very helpful, and instructive. Thanks a lot, i will surely try this on.

  • Internet Marketing Consultant said:

    Very convenient code, i would say you did a commendable job by writting and sharing these codes.

  • wu yi tea said:

    This code is really of great help , and linking up is so important in the websites.
    thanks for sharing.

    wu yi teas latest published blog-post ………. Day Fourteen – Two Weeks and Thirteen Pounds

  • Gregor@Smartphones & Accessories said:

    In addition I would suggest to read some documention about how to create a theme on WordPress official site.

    Gregors latest published blog-post ………. Nokia: Global mobile phone market will fall in 2009

  • Website Design Midlands said:

    Thanks for sharing these codes, i really have a pain writting codes, thanks a lot again !

  • reverse cellphone lookup said:

    Very serviceable codes, and they are easy to implement as well.. thanks.

  • Cheap Flights to Mallorca said:

    These code proved really useful to me, Time was really limited and i couldn’t write codes so quickly, im glad you shared these codes and i came across it. thanks a lot.

  • Fling,com said:

    Very beneficial and serviceable codes. thanks alot for sharing.

  • Joel said:

    Hello I need your help!!!!
    In my index.php..I confused because i already coded the previous_posts_link()…lines,but I cant see it.Is that possible that I see the the links even in I’m in the local..

  • UNOBLOGGER@make money online said:

    Very nice post… this sure is something too technical for people like me, aren’t there any plugins?

    UNOBLOGGERs latest published blog-post ………. What are Keywords?

  • Prepaid Phone Wireless said:

    Can you provide more information on this? I was looking for this additional information, just bookmarked to check the updates :)

  • Abd el rahman said:

    thanks alot for sharing

  • LOCC - Tha Most Gangsta said:

    To display the previous and next post links (where the “next” post is a .. that will display the navigation only if a previous or next entry exists, plugin WP-PageNavi, que, no caso do modelo de índice do blog.

  • How to edit pictures said:

    First time i tried it didn`t work (I blew it somewhere), so i tried again. And it worked. Thanks.

  • Tim@Best List Online said:

    Ah, great post. I do not do this on my blog and I should. It entices the reader to stay on your blog. Thanks for the how-to!

  • Asif@Symbian Smartphone said:

    Great tutorial brother. Working perfectly with my wodpress blog 2.7

    Asifs latest published blog-post ………. GreenPhone | Battery Extender | Nokia

  • Joomal Expert said:

    Hello…

    I am working on PHP. I will defiantly use your tips for creating my blog. You have give very nice information. Thanks for sharing with us such a nice post.

    Joomla Expert

  • Hospitalitystaff said:

    Wow………….,nice post
    I can see you have put a lot of work into it.I have being reading your posts about. I can see how this benefits everyone.Thankyou

  • propertyforsalejavea said:

    A very informative and nice post. Thanks for sharing with us.
    .-= propertyforsalejavea´s last blog ..Property For Sale In Javea =-.

  • el acido folico said:

    very interesting post and very usefull
    thank you for sharing
    .-= el acido folico´s last blog ..¡Hola, mundo! =-.

  • MacPress said:

    Interesting, never hear this before but don’t assume that just because you agree with.
    .-= MacPress´s last blog ..Hello world! =-.

  • MacPress said:

    Interesting, never hear this before but don’t assume that just because you agree with.

  • Magazin Haberleri said:

    dealing with wordpress is sometimes easy but this is interesting. thanks
    .-= Magazin Haberleri´s last blog ..Leona Lewis’e Saldırı! =-.

  • Ganry81 said:

    Is it an open source project or not? ,

  • Barbara98 said:

    There is a place for Head Start. ,

  • Mark66 said:

    What connectivity will they have? ,

  • Loy80 said:

    Broil until cheese is browned and zucchini is barely tender, about 5-7 minutes. ,

  • msn adresleri said:

    very interesting post and very usefull thank you for article.
    .-= msn adresleri´s last blog ..Cinsel uyarıcı ve hormon dengeleyici =-.

  • Lea said:

    This is one of the features that are being neglected by webmasters. I’ll definitely add this buttons in my blog. Thanks for the code.
    .-= Lea´s last blog ..Resellers =-.

  • Chris - Bermuda Triangle said:

    Thanks, the info was helpful because i use wordpress to make some of my blogs.
    .-= Chris – Bermuda Triangle´s last blog ..Thermometer Clocks =-.

  • Asfand Mudassir said:

    Post that you have posted is very useful, can we use it in blogspot.please reply..thanks

  • Erken rezervasyon said:

    Thank you for your informative article and comments

  • Moto Kurye said:

    LOL – love the phrase ‘mango-fu’ – gonna use both the phrase and the technique myself now :)
    .-= Moto Kurye´s last blog ..Yıldız Kurye =-.

  • conversation said:

    nice article,thanks for the tips .. they were useful like the everyone said

  • Yani said:

    I’ll try to apply this type of programming in my next project. Thanks for sharing this info.

  • acompanhantes said:

    I really enjoyed your article, I tweet!

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.