2013-10-21

Created page with "{{Copyedit}} __FORCETOC__ The '''Next''' and '''Previous''' post links guide your visitor through your WordPress site. When it comes to creating [[Good_Navigation_Links|strong …"

New page

{{Copyedit}}

__FORCETOC__

The '''Next''' and '''Previous''' post links guide your visitor through your WordPress site. When it comes to creating [[Good_Navigation_Links|strong site-wide navigation]], some of the most powerful tools for moving your visitor around are these link tags.

There are two sets of tags that move the visitor through your WordPress site: <tt>posts_nav_link()</tt>, which displays both the '''Previous''' and '''Next''' links, and the combination pair of <tt>previous_post()</tt> and <tt>next_post()</tt>, which each display one of the '''Previous''' or '''Next''' links. This article will look at how these two tag sets work.

Note: "Previous" and "Next" in this case refer to posts in the order that they are in, not to any particular direction in time. This often confuses many people, as WordPress, by default, displays posts starting from the newest and proceeding backwards in time. Using this default ordering, "Next" would be moving backwards in time, because the "next" page after page 1 would be page 2, and that would move to older posts. If the post ordering is changed (like via a manual usage of [[Template_Tags/query_posts|query_posts]] in a template), then the links will point in different directions. This codex article uses both methods without explanation, because it is example code only. So it is important to keep in mind that the function is referring to an order that is independent of chronological time.

= The posts_nav_link =

The first set of these site navigation links is featured only on the non-single/non-permalink web pages, such as categories, archives, searches, and the index page. It is the template tag [[Template_Tags/posts_nav_link|posts_nav_link()]]. This tag creates two links at the bottom of the page within the [[The_Loop|WordPress Loop]] to display the '''next''' and '''previous''' pages in chronological order.

By default, the <tt>posts_nav_link</tt> looks like this:

<div style="border:1px solid blue; width:50%; margin:20px; padding:20px">[[#The posts_nav_link|« Previous Page]] — [[#The posts_nav_link|Next Page »]]</div>

It is often found in a paragraph code or a division like this:

<pre><div class="navigation"><p><?php posts_nav_link(); ?></p></div></pre>

The parameters of the tag are as follows:

<pre><?php posts_nav_link('separator','prelabel','nextlabel'); ?></pre>

Each of these parameters can be used to generate a ''string'' or any text or HTML or CSS tags. Let's see what we can do to make these post navigation links more interesting.

Keeping things simple, we could just change the look of the tag results [[Blog_Design_and_Layout|using CSS]]. Let's go farther and also change the content within the tag's parameters.

Next, make the text bold and use the <tt>font-variant: small-caps</tt> to make it look interesting, and then make the ''separator'' the [[Fun_Character_Entities|infinity symbol]] and add some words to the labels.

<pre><div class="navigation"><p><?php posts_nav_link('&#8734;','Go

Forward In Time','Go Back in Time'); ?></p></div></pre>

And it might look like this:

<div style="border:1px solid blue; width:50%; margin:20px; padding:20px; font-size:120%; font-variant:small-caps; font-weight: bold">[[#The posts_nav_link|Go Forward in Time]] ∞ [[#The posts_nav_link|Go Back in Time]]</div>

Let's not stop there, let's add more [[Fun_Character_Entities|character entities]] to really get the visitor's attention so they understand that there is more to your site than what they see on this page.

<pre><div class="navigation"><p><?php posts_nav_link('&#8734;','&laquo;&laquo; Go Forward

In Time','Go Back in Time &raquo;&raquo;'); ?></p></div></pre>

And it might look like this:

<div style="border:1px solid blue; width:50%; margin:20px; padding:20px; font-size:120%; font-variant:small-caps; font-weight: bold">[[#The posts_nav_link|«« Go Forward in Time]] ∞ [[#The posts_nav_link|Go Back in Time »»]]</div>

We have just uncovered the surface, but you can let your imagination and web page design skills create these any way you like, adding borders, background images, stylized text, and more.

=The Next and Previous Posts=

The other set of navigational aids for moving through your site control the '''next post''' and '''previous post''' links typically found at the bottom of your single/permalink post, such as any single post or article you have published on your site. These direct the user to move to the next or previous post in chronological order.

The template tags are [[Template_Tags/previous_post|previous_post()]] and [[Template_Tags/next_post|next_post()]].

<div style="border:1px solid red; padding:10px;">

'''Deprecated : Use: --> [http://codex.wordpress.org/Template_Tags/previous_post_link previous_post_link] and [http://codex.wordpress.org/Template_Tags/next_post_link next_post_link] instead.'''

</div>

The default usage of these tags are:

<?php previous_post(); ?> <?php next_post(); ?>

And it looks like this:

<div style="border:1px solid blue; width:80%; padding:10px">[[#The Next and Previous Posts|previous post: A Story for One and All]]    [[#The Next and Previous Posts|next post: A Story for Only One]]</div>

The parameters for both of these tags are:

;format : Text used in combination with the '%' to represent the permalink to the post. The default is the permalink.

;text : Text displayed before the permalink. The default is "next post" and "previous post".

;title : This turns "on" and "off" the use of the post title to be used as the link text. By default, is it "yes". If set to "no", then only the text set in the ''text'' parameter and ''format'' would show.

Let's put these into action.

The following example displays the next and previous post titles with [[Fun_Character_Entities|arrows]] to emphasis the direction the user may choose. You will notice that we have not set the ''text'' parameter, so it will be blank.

<pre><?php previous_post('&laquo; &laquo; %', '', 'yes'); ?>

| <?php next_post('% &raquo; &raquo; ', '', 'yes'); ?></pre>

<div style="border:1px solid blue; width:80%; margin:20px; padding:20px">[[#The Next and Previous Posts|« « A Story for One and All]]    |    [[#The Next and Previous Posts|A Story for One » »]]</div>

Wrap these two tags with CSS and you can do even more with these tags:

<pre><div class="navigation">

<div class="alignleft">

<?php previous_post('&laquo; &laquo; %',

'Toward The Past: ', 'yes'); ?>

</div>

<div class="alignright">

<?php next_post('% &raquo; &raquo; ',

'Toward The Future: ', 'yes'); ?>

</div>

</div> <!-- end navigation --></pre>

And it might look like this:

<div style="border:1px solid blue; width:90%; margin:20px; padding:20px; font-size:120%; font-variant:small-caps; font-weight: bold">[[#The Next and Previous Posts|« « Toward the Past: A Story for One and All]]       [[#The Next and Previous Posts|Toward the Future: A Story for One » »]]</div>

A useful plugin called "[http://wordpress.org/extend/plugins/better-nearby-posts-links/ Better Adjacent Post Links]" allows you to trim the title of the previous and next posts to any length you see fit. This is useful when you have longer titles that break the site's design.

This is just an introduction on how to use these tags and do fun things with them, but you can do so much more by adding borders, background images, interesting fonts and colors - it's up to you! Have fun!

=The Next and Previous Post=

This is the custom Option to show the Next Post and Previous Post without using the predefined function like [http://codex.wordpress.org/Template_Tags/previous_post_link previous_post_link] and [http://codex.wordpress.org/Template_Tags/next_post_link next_post_link]. I think this thread which is useful for most Plugin Developers.

<pre><?php

$postlist = get_posts('sort_column=menu_order&sort_order=asc');

$posts = array();

foreach ($postlist as $post) {

$posts[] += $post->ID;

}

$current = array_search(get_the_ID(), $posts);

$prevID = $posts[$current-1];

$nextID = $posts[$current+1];

?>

<div class="navigation">

<?php if (!empty($prevID)) { ?>

<div class="alignleft">

<a href="<?php echo get_permalink($prevID); ?>"

title="<?php echo get_the_title($prevID); ?>">Previous</a>

</div>

<?php }

if (!empty($nextID)) { ?>

<div class="alignright">

<a href="<?php echo get_permalink($nextID); ?>"

title="<?php echo get_the_title($nextID); ?>">Next</a>

</div>

<?php } ?>

</div><!-- .navigation --></pre>

=The Next and Previous Pages=

The [http://codex.wordpress.org/Template_Tags/previous_post_link previous_post_link] and [http://codex.wordpress.org/Template_Tags/next_post_link next_post_link] functions don't work on pages. The [http://wordpress.org/extend/plugins/next-page/ Next Page] and [http://wordpress.org/extend/plugins/next-page-not-next-post/ Next Page, Not Next Post] plugins work around this problem.

If you would prefer to add the code to your theme's page template instead:

<pre><?php

$pagelist = get_pages('sort_column=menu_order&sort_order=asc');

$pages = array();

foreach ($pagelist as $page) {

$pages[] += $page->ID;

}

$current = array_search(get_the_ID(), $pages);

$prevID = $pages[$current-1];

$nextID = $pages[$current+1];

?>

<div class="navigation">

<?php if (!empty($prevID)) { ?>

<div class="alignleft">

<a href="<?php echo get_permalink($prevID); ?>"

title="<?php echo get_the_title($prevID); ?>">Previous</a>

</div>

<?php }

if (!empty($nextID)) { ?>

<div class="alignright">

<a href="<?php echo get_permalink($nextID); ?>"

title="<?php echo get_the_title($nextID); ?>">Next</a>

</div>

<?php } ?>

</div><!-- .navigation --></pre>

== Resources ==

* [http://www.ericmmartin.com/conditional-pagepost-navigation-links-in-wordpress-redux/ Conditional page/post navigation links]

[[Category:WordPress Lessons]]

[[Category:Design and Layout]]

Show more