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.
Thanks for this and for being “do follow”. 🙂
RT Cunninghams last blog post..Building Backlinks with Blogs that Do Follow, not No Follow
Perfect, thanks a lot.
VOTED for you at NewsDot
Geoservs last blog post..Trackback?
Hey look at me! I’m spamming your blog with my comment! Can you do the same? – Howie
Now that\’s something to think about. Glad I stopped by – keep up the good work.Ciao!
Cheers for the info, will come in handy with my new website project
Very convenient code, i would say you did a commendable job by writting and sharing these codes.
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
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
Thanks for sharing these codes, i really have a pain writting codes, thanks a lot again !
Very beneficial and serviceable codes. thanks alot for sharing.
What connectivity will they have? ,
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..
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?
thanks alot for sharing
First time i tried it didn`t work (I blew it somewhere), so i tried again. And it worked. Thanks.
Great tutorial brother. Working perfectly with my wodpress blog 2.7
Asifs latest published blog-post ………. GreenPhone | Battery Extender | Nokia
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
There is a place for Head Start. ,
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
Is it an open source project or not? ,
Broil until cheese is browned and zucchini is barely tender, about 5-7 minutes. ,
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 =-.
Thank you for your informative article and comments
nice article,thanks for the tips .. they were useful like the everyone said
I’ll try to apply this type of programming in my next project. Thanks for sharing this info.
I really enjoyed your article, I tweet!