<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Digging into WordPress &#187; navigation</title>
	<atom:link href="http://digwp.com/tag/navigation/feed/" rel="self" type="application/rss+xml" />
	<link>http://digwp.com</link>
	<description>Take your WordPress skills to the next level.</description>
	<lastBuildDate>Fri, 18 May 2012 18:21:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Next/Previous Post Navigation Outside of the WordPress Loop</title>
		<link>http://digwp.com/2010/04/post-navigation-outside-loop/</link>
		<comments>http://digwp.com/2010/04/post-navigation-outside-loop/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 12:50:50 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[navigation]]></category>
		<category><![CDATA[posts]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1975</guid>
		<description><![CDATA[WordPress provides several navigational template tags to make it easy for visitors to surf your pages. There are basically two different types of template tags used for chronological post navigation: posts_nav_link() &#8211; for navigating various archive (non-single) pages previous_post_link() &#38; next_post_link() &#8211; for navigating single-post pages These template tags output the HTML markup required to [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress provides several <a href="http://digwp.com/2009/08/wordpress-page-navigation/" title="Definitive Guide to WordPress Page Navigation">navigational template tags</a> to make it easy for visitors to surf your pages. There are basically two different types of template tags used for chronological post navigation:</p>
<ul>
<li><code>posts_nav_link()</code> &ndash; for navigating various archive (non-single) pages</li>
<li><code>previous_post_link()</code> &amp; <code>next_post_link()</code> &ndash; for navigating single-post pages</li>
</ul>
<p>These template tags output the <acronym title="Hypertext Markup Language">HTML</acronym> markup required to create the actual hyperlinks that will appear on your web pages. By default, navigation links for single pages display the title of adjacent posts, while those for archive pages display &ldquo;Next-Page&rdquo; and &ldquo;Previous-Page&rdquo;.</p>
<p>Of course, the default text, link-position, and just about everything else can be <a href="http://digwp.com/2009/12/optimizing-wordpress-post-navigation/" title="Optimizing WordPress Post Navigation">customized and optimized</a> according to your specific needs.</p>
<p><span id="more-1975"></span></p>
<p>Normally, WordPress&rsquo; navigational template tags appear within the loop:</p>
<div id="post_1975">
<pre><code>&lt;?php get_header(); ?&gt;

	&lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;

		&lt;h1&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;
		&lt;?php the_content(); ?&gt;

	&lt;?php endwhile; ?&gt;

		&lt;p class="nav"&gt;&lt;?php posts_nav_link(); ?&gt;&lt;/p&gt;

	&lt;?php else : ?&gt;

		&lt;h1&gt;Nothing Found&lt;/h1&gt;
		&lt;p&gt;Please try a little harder next time..&lt;/p&gt;

	&lt;?php endif; ?&gt;

&lt;?php get_footer(); ?&gt;</code></pre>
</div>
<p>This code is typical for <code>index.php</code> and other &ldquo;Archive-View&rdquo; pages, such as for Categories, Tags, Search Results, and so on. Similar code is used for Single Page-Views, where <code>single.php</code> would replace the <code>posts_nav_link()</code>&nbsp;<sup>1</sup> template tag for <code>previous_post_link()</code> and <code>next_post_link()</code> tags.</p>
<p>The navigational template tags are generally placed between the <code>endwhile</code> and <code>else</code>&nbsp;<sup>2</sup> statements, along with stuff like post comments, meta information, and social-media links. Within this section of the loop, code is processed only once, as opposed to multiple times in the first part of the loop.</p>
<h3>Navigation <em>outside</em> of the loop</h3>
<p>The good news is that the template tag for archive-view navigation (i.e., non-single pages) seems to work just fine outside of the loop. So you can just place <code>posts_nav_link()</code> anywhere within your theme template.</p>
<p>Then, to include <strong>single-view</strong> navigation outside of the loop, we can use <code>query_posts</code> to substantiate the loop anywhere within your <code>single.php</code> file. Here is a simplified version of the code that I am using at <a href="http://perishablepress.com/" title="Digital Design and Dialogue">Perishable Press</a>:</p>
<pre><code>&lt;?php $posts = query_posts($query_string); if (have_posts()) : while (have_posts()) : the_post(); ?&gt;

	&lt;?php previous_post_link(); ?&gt; | &lt;?php next_post_link(); ?&gt;

&lt;?php endwhile; endif; ?&gt;</code></pre>
<p>That&rsquo;s all there is to it, really. A nice trick to have in the hat. Then, we can take it a step further and consolidate both types of page navigation (single and archive) in a single chunk of code:</p>
<pre><code>&lt;?php if(is_single()) { // single-view navigation ?&gt;

	&lt;?php $posts = query_posts($query_string); if (have_posts()) : while (have_posts()) : the_post(); ?&gt;

		&lt;?php previous_post_link(); ?&gt; | &lt;?php next_post_link(); ?&gt;

	&lt;?php endwhile; endif; ?&gt;

&lt;?php } else { // archive view navigation ?&gt;

		&lt;?php posts_nav_link(); ?&gt;

&lt;?php } ?&gt;</code></pre>
<p>This code displays the appropriate template tags for both single-page-views and archive-views. If placed in its own file named &ldquo;<code>nav.php</code>&rdquo;, including it within your theme files is as simple as a template tag:</p>
<pre><code>&lt;?php include_once("nav.php"); ?&gt;</code></pre>
<p>That&rsquo;s an easy way to get your Next/Previous page navigation working anywhere outside the loop. This could easily be made into a <a href="http://digwp.com/2010/03/wordpress-functions-php-template-custom-functions/" title="WordPress functions.php Template with 15 Essential Custom Functions">custom function</a> for your theme&rsquo;s <a href="http://digwp.com/2010/04/wordpress-custom-functions-php-template-part-2/" title="WordPress Custom functions.php Template, Part 2">functions.php file</a>. Or maybe even <a href="http://digwp.com/2010/03/add-plugin-to-wordpress-plugin-repository/" title="How to Add Your Plugin to the WordPress Plugin Directory">a plugin</a>..?&nbsp;;)</p>
<p>For more information on WordPress Page Navigation, check out these fine <acronym title="Digging into WordPress">DiW</acronym> articles:</p>
<ul>
<li><a href="http://digwp.com/2009/08/wordpress-page-navigation/">Definitive Guide to WordPress Page Navigation</a></li>
<li><a href="http://digwp.com/2009/12/optimizing-wordpress-post-navigation/">Optimizing WordPress Post Navigation</a></li>
</ul>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/04/post-navigation-outside-loop/">Permalink</a> | <a href="http://digwp.com/2010/04/post-navigation-outside-loop/#comments">15 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/04/post-navigation-outside-loop/&title=Next/Previous Post Navigation Outside of the WordPress Loop">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/navigation/" rel="tag">navigation</a>, <a href="http://digwp.com/tag/posts/" rel="tag">posts</a>, <a href="http://digwp.com/tag/tips/" rel="tag">tips</a>, <a href="http://digwp.com/tag/tricks/" rel="tag">tricks</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/04/post-navigation-outside-loop/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Optimizing WordPress Post Navigation</title>
		<link>http://digwp.com/2009/12/optimizing-wordpress-post-navigation/</link>
		<comments>http://digwp.com/2009/12/optimizing-wordpress-post-navigation/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 06:20:55 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[navigation]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[posts]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1058</guid>
		<description><![CDATA[Implementing a solid set of navigational links for your WordPress site is one of the best ways to encourage visitors to stick around awhile and check out additional content. As discussed in our definitive guide to WordPress post navigation, there are essentially three different types of navigational tags for WordPress: Index and archive navigation: posts_nav_link() [...]]]></description>
			<content:encoded><![CDATA[<p>Implementing a solid set of navigational links for your WordPress site is one of the best ways to encourage visitors to stick around awhile and check out additional content. As discussed in our <a href="http://digwp.com/2009/08/wordpress-page-navigation/" title="Definitive Guide to WordPress Page Navigation">definitive guide to WordPress post navigation</a>, there are essentially three different types of navigational tags for WordPress:</p>
<ul>
<li>Index and archive navigation: <a href="http://digwp.com/2009/08/wordpress-page-navigation/#postsnavlink" title="posts_nav_link()">posts_nav_link()</a></li>
<li>Index and archive navigation: <a href="http://digwp.com/2009/08/wordpress-page-navigation/#previouspostslink" title="previous_posts_link() and next_posts_link()">previous_posts_link() and next_posts_link()</a></li>
<li>Single-view posts navigation: <a href="http://digwp.com/2009/08/wordpress-page-navigation/#previouspostlink" title="previous_post_link() and next_post_link()">previous_post_link() and next_post_link()</a></li>
</ul>
<p>Without repeating ourselves, suffice it to say that <code>previous_posts_link</code> and <code>next_posts_link</code> are perfect for setting up separate links for index, category, and archive pages. Likewise, <code>previous_post_link</code> and <code>next_post_link</code> are perfect for setting up separate links for single-view post navigation.</p>
<p>Now that we&rsquo;re on the same page, so to speak, let&rsquo;s look at how these tags are typically used and how to improve and optimize their functionality..</p>
<p><span id="more-1058"></span></p>
<h3>Improving the <code>next_posts_link</code> and <code>previous_posts_link</code> (for archive views)</h3>
<p>Here are the archive/category navigation links as provided in the default theme that is packaged with WordPress:</p>
<pre><code>&lt;div class="navigation"&gt;
	&lt;div class="alignleft"&gt;&lt;?php next_posts_link('&amp;laquo; Older Entries') ?&gt;&lt;/div&gt;
	&lt;div class="alignright"&gt;&lt;?php previous_posts_link('Newer Entries &amp;raquo;') ?&gt;&lt;/div&gt;
&lt;/div&gt;</code></pre>
<p>Functionally, this setup works great &#8212; the previous-post link is displayed <em>only</em> when there are previous posts, and likewise the next-post link is displayed <em>only</em> when there are newer posts. As convenient as this is, it doesn&rsquo;t help us with the accompanying markup used to display the links. Any <acronym title="Cascading Style Sheets">CSS</acronym> styles applied to the <code>&lt;div&gt;</code>s in our example will be displayed even when the links themselves aren&rsquo;t displayed. This can result in several issues, including:</p>
<ul>
<li>Padding and margins may prevent proper display when links are missing</li>
<li>Background images used to enhance the navigational links will still be displayed</li>
<li>Additional text and/or characters will still be displayed even when links absent</li>
<li>Use of any markup other than <code>&lt;div&gt;</code>s may result in validation errors when left empty</li>
</ul>
<p>And we&rsquo;re not just talking about the front page and last page here &#8212;  any category, tag, search, date, or author archive will also display botched navigational elements and design flaws when previous and next posts are unavailable. Your main index may always have previous posts, but what about that guest author archive, 2005 archive, and search results? Such pages are frequently one of a kind. Even worse, by defualt, your Home page will <em>never</em> have a page after it, which means a broken navigational element right there on the most important page of your site. Not good.</p>
<p>The easiest way that I have found to prevent this from happening when the &ldquo;next&rdquo; link is not generated is to simply test the <code>$paged</code> variable for a value greater than <code>1</code>:</p>
<pre><code>&lt;?php if ($paged &gt; 1) { ?&gt;

	&lt;div class="navigation"&gt;
		&lt;div class="alignleft"&gt;&lt;?php next_posts_link('&amp;laquo; Older Entries') ?&gt;&lt;/div&gt;
		&lt;div class="alignright"&gt;&lt;?php previous_posts_link('Newer Entries &amp;raquo;') ?&gt;&lt;/div&gt;
	&lt;/div&gt;

&lt;?php } ?&gt;</code></pre>
<p>By checking if there are more posts to display before calling the navigational code, this technique will prevent empty elements from wrinkling your home page and archive pages. Another way of doing this is shared by <a href="http://www.ericmmartin.com/conditional-pagepost-navigation-links-in-wordpress-redux/" title="Conditional page/post navigation links in WordPress (redux)">Eric Martin</a>. Just place this snippet into your active theme&rsquo;s <code>functions.php</code> file:</p>
<pre><code>// return TRUE if more than one page exists
function show_posts_nav() {
	global $wp_query;
	return ($wp_query-&gt;max_num_pages &gt; 1);
}</code></pre>
<p>And then use the function to conditionally display navigational code in your <code>archive.php</code> and other archive-view template files:</p>
<pre><code>&lt;?php if (show_posts_nav()) : ?&gt;

	&lt;div class="navigation"&gt;
		&lt;div class="alignleft"&gt;&lt;?php next_posts_link('&amp;laquo; Older Entries') ?&gt;&lt;/div&gt;
		&lt;div class="alignright"&gt;&lt;?php previous_posts_link('Newer Entries &amp;raquo;') ?&gt;&lt;/div&gt;
	&lt;/div&gt;

&lt;?php endif; ?&gt;</code></pre>
<p>Not sure if there is any benefit to adding the extra code in the <code>functions.php</code> file, but I thought I would share that technique with you as another possible solution (hey you never know).</p>
<p>Also, a great way to improve the functionality and presentation of your blog is to provide some alternate navigational elements to help keep things flowing and balanced on the front pages of your site. For example, at <a href="http://perishablepress.com/" title="Digital Design and Dialogue">Perishable Press</a>, if the &ldquo;next&rdquo; link isn&rsquo;t displayed, I provide a link to my site Archives:</p>
<pre><code>&lt;?php if ($paged &gt; 1) { ?&gt;

	&lt;div class="navigation"&gt;
		&lt;div class="alignleft"&gt;&lt;?php next_posts_link('&amp;laquo; Older Entries') ?&gt;&lt;/div&gt;
		&lt;div class="alignright"&gt;&lt;?php previous_posts_link('Newer Entries &amp;raquo;') ?&gt;&lt;/div&gt;
	&lt;/div&gt;

&lt;?php } else { ?&gt;

	&lt;div class="navigation"&gt;
		&lt;div class="alignleft"&gt;&lt;?php next_posts_link('&amp;laquo; Older Entries') ?&gt;&lt;/div&gt;
		&lt;div class="alignright"&gt;&lt;a href="http://perishablepress.com/press/archives/"&gt;Site Archives &amp;raquo;&lt;/a&gt;&lt;/div&gt;
	&lt;/div&gt;

&lt;?php } ?&gt;</code></pre>
<p>And of course, anything is possible here; the key is to keep things consistent, logical, and well-styled. Now let&rsquo;s see how to improve navigational display for single-post views.</p>
<h3>Improving the <code>next_post_link</code> and <code>previous_post_link</code> (for single views)</h3>
<p>Fortunately, the situation is much improved for single-post navigation. By default, the <code>next_post_link</code> and <code>previous_post_link</code> provide parameters that enable us to avoid the &ldquo;empty-element&rdquo; syndrome. Let&rsquo;s take a look at these parameters:</p>
<p><code>&lt;?php next_post_link('format','link','in_same_cat','excluded_categories'); ?&gt;</code><br />
<code>&lt;?php previous_post_link('format','link','in_same_cat','excluded_categories'); ?&gt;</code></p>
<p>The <code>format</code> and <code>link</code> parameters enable us to do something like this:</p>
<pre><code>&lt;div class="navigation"&gt;

	&lt;?php previous_post_link('&lt;div class="alignleft"&gt;Previous entry: %link&lt;/div&gt;', '%title'); ?&gt;
	&lt;?php next_post_link('&lt;div class="alignright"&gt;Next entry: %link&lt;/div&gt;', '%title'); ?&gt;

&lt;/div&gt;</code></pre>
<p>Here we have replicated the markup in our previous examples, but have done so conditionally, such that the markup will only be displayed when the corresponding link is displayed, thereby enabling us to avoid empty elements when either the previous or next post-link is not displayed. As you can see, the single-post nav tags enable us to include the surrounding <acronym title="(eXtensible) Hypertext Markup Language">(X)HTML</acronym> and text <em>within</em> the function itself. We can do similar tricks with the actual link title. Check out our <a href="http://digwp.com/2009/08/wordpress-page-navigation/" title="Definitive Guide to WordPress Page Navigation">definitive guide to post navigation</a> for more information.</p>
<h3>Conditional navigational display for single posts</h3>
<p>Just as we did with the archive nav tags (<code>next_posts_link</code> and <code>previous_posts_link</code>), we can implement some conditional functionality to display alternate navigational links when viewing the first and last posts in any archive view. For example, if I also wanted to provide a link to my Site Archives when the &ldquo;next&rdquo; and/or &ldquo;previous&rdquo; links were <strong>not</strong> displayed, I could do so using the following code:</p>
<pre><code>&lt;div class="navigation"&gt;

	&lt;?php previous_post_link('&lt;div class="alignleft"&gt;Previous entry: %link&lt;/div&gt;', '%title'); ?&gt;
	&lt;?php if(!get_adjacent_post(false, '', true)) { 
		echo '&lt;div class="alignleft"&gt;&lt;a href="http://perishablepress.com/press/archives/"&gt;Site Archives&lt;/a&gt;&lt;/div&gt;'; 
	} ?&gt;

	&lt;?php next_post_link('&lt;div class="alignright"&gt;Next entry: %link&lt;/div&gt;', '%title'); ?&gt;
	&lt;?php if(!get_adjacent_post(false, '', false)) { 
		echo '&lt;div class="alignright"&gt;&lt;a href="http://perishablepress.com/press/archives/"&gt;Site Archives&lt;/a&gt;&lt;/div&gt;'; 
	} ?&gt;

&lt;/div&gt;</code></pre>
<p>Here we are using the <code>get_adjacent_post</code> function to determine whether or not there is a post before/after the current post. This enables us to conditionally display some sort of fallback navigation in the first- and last-post case. The <code>get_adjacent_post</code> tag accepts the following parameters:</p>
<ul>
<li><code>$in_same_cat</code> &ndash; specifies whether or not the adjacent post should be in the same category</li>
<li><code>$excluded_categories</code> &ndash; a comma-delimited list of categories for which the adjacent post should not belong</li>
<li><code>$previous</code> &ndash; specifies whether the previous post should be displayed</li>
</ul>
<p>That does it for the single-post views. Let&rsquo;s wrap things up with a way to consolidate and streamline the single and archive navigational methods..</p>
<h3>Optimize your theme&rsquo;s navigational code</h3>
<p>Finally, here is a nice way to clean up your theme files and source code. The first step is to streamline production by <a href="http://digwp.com/2009/07/getting-more-fine-grained-with-includes/" title="Getting More Fine-Grained with Includes">getting more fine-grained with includes</a>. This basically means that you can clean things up and save time/effort by moving your navigational code to its own file, named something like &ldquo;<code>navigation.php</code>&rdquo; or similar. You would then remove your nav code from your other template files and consolidate them all into that one location. You would then include <code>navigation.php</code> in any theme file where you want the navigation section to appear:</p>
<pre><code>&lt;?php include_once("navigation.php"); ?&gt;</code></pre>
<p>This technique makes maintaining and modifying your code a breeze. And, you can even streamline things further by using some conditional logic to display either <em>archive</em> or <em>single</em> navigation depending on the current page being viewed:</p>
<pre><code>&lt;?php if (is_single()) : ?&gt;

	&lt;div class="navigation"&gt;
		&lt;?php previous_post_link('&lt;div class="alignleft"&gt;Previous entry: %link&lt;/div&gt;', '%title'); ?&gt;
		&lt;?php next_post_link('&lt;div class="alignright"&gt;Next entry: %link&lt;/div&gt;', '%title'); ?&gt;
	&lt;/div&gt;

&lt;?php else : ?&gt;

	&lt;div class="navigation"&gt;
		&lt;div class="alignleft"&gt;&lt;?php next_posts_link('&amp;laquo; Older Entries') ?&gt;&lt;/div&gt;
		&lt;div class="alignright"&gt;&lt;?php previous_posts_link('Newer Entries &amp;raquo;') ?&gt;&lt;/div&gt;
	&lt;/div&gt;

&lt;?php endif; ?&gt;</code></pre>
<p>This is a great way of staying organized and reducing your workload during theme development and maintenance.</p>
<h3>Wrap up</h3>
<p>The tips and tricks in this article will help you create a consistent, logical, and design-friendly set of navigational links for your next theme design. In the process, you can also streamline production by consolidating your code into a single file and using conditional logic to deliver the appropriate code.</p>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/12/optimizing-wordpress-post-navigation/">Permalink</a> | <a href="http://digwp.com/2009/12/optimizing-wordpress-post-navigation/#comments">10 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/12/optimizing-wordpress-post-navigation/&title=Optimizing WordPress Post Navigation">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/navigation/" rel="tag">navigation</a>, <a href="http://digwp.com/tag/optimization/" rel="tag">optimization</a>, <a href="http://digwp.com/tag/posts/" rel="tag">posts</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/12/optimizing-wordpress-post-navigation/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Definitive Guide to WordPress Page Navigation</title>
		<link>http://digwp.com/2009/08/wordpress-page-navigation/</link>
		<comments>http://digwp.com/2009/08/wordpress-page-navigation/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 05:03:47 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[navigation]]></category>
		<category><![CDATA[tags]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=560</guid>
		<description><![CDATA[There are many ways to navigate a WordPress-powered site. There are archive links, category links, page links, internal post links, single post links, admin comment links, tag links, and many other types of navigational links. When it comes to navigating sequentially through your site&#8217;s chronological archive pages, category archives, and other types of archive pages, [...]]]></description>
			<content:encoded><![CDATA[<p>There are many ways to navigate a WordPress-powered site. There are archive links, category links, page links, internal post links, single post links, admin comment links, tag links, and many other types of navigational links. When it comes to navigating sequentially through your site&rsquo;s chronological archive pages, category archives, and other types of archive pages, WordPress provides several useful template tags designed to dynamically link the pages together. Likewise, for single permalink post-views, WordPress provides a set of template tags that connects the pages together in chronologically sequential fashion.</p>
<p>When used properly, these page-navigation template tags enable your visitors to browse your site quickly and easily. Unfortunately, these different template tags have similar names, parameters, and functionality, thereby leaving many WordPress designers dazed and confused. </p>
<blockquote><p>In this <acronym title="Digging into WordPress">DiW</acronym> article, we&rsquo;ll dig into each of these different navigational tags and explain each of them in-depth, with plenty of juicy examples to help you take advantage of their full functionality.</p></blockquote>
<p>Here are the three sets of template tags we&rsquo;ll be exploring (click on a tag name to jump to that section):</p>
<p><span id="more-560"></span></p>
<ul id="navmenu">
<li>Index and archive navigation: <a href="#postsnavlink" title="posts_nav_link()">posts_nav_link()</a></li>
<li>Index and archive navigation: <a href="#previouspostslink" title="previous_posts_link() and next_posts_link()">previous_posts_link() and next_posts_link()</a></li>
<li>Single-view posts navigation: <a href="#previouspostlink" title="previous_post_link() and next_post_link()">previous_post_link() and next_post_link()</a></li>
</ul>
<h3 id="postsnavlink">posts_nav_link() &#8212; paired links for index, category, and archive pages</h3>
<p><small>[&nbsp;<a href="#navmenu" title="Return to post menu">&uarr;</a>&nbsp;]</small> <strong>The <code>posts_nav_link()</code> template tag provides a combined pair of &ldquo;next&rdquo; and &ldquo;previous&rdquo; navigational links for any type of paged archive view.</strong> Basically, this tag creates paired navigational links for anything other than single-post views. This includes index, category, archive, tag, and search pages. Best results when used either before or after the loop. Here is an example of the tag showing its three accepted parameters:</p>
<pre><code>&lt;?php posts_nav_link('sep','prelabel','nxtlabel'); ?&gt;</code></pre>
<h4>Default usage</h4>
<p>Here is the default usage of this template tag, followed by its corresponding output:</p>
<p><code>&lt;?php posts_nav_link(); ?&gt;</code></p>
<p><a href="#">&laquo; Previous Page</a> &#8212; <a href="#">Next Page &raquo;</a></p>
<h4>Parameters</h4>
<p>Values for each of the following parameters are strings that may be specified in alphanumeric characters or <acronym title="(eXtensible) Hypertext Markup Language">(X)HTML</acronym> markup:</p>
<p><code>&lt;?php posts_nav_link('sep','prelabel','nxtlabel'); ?&gt;</code></p>
<ul>
<li><code>sep</code> &#8212; text/markup that is displayed between the links</li>
<li><code>prelabel</code> &#8212; text/markup that is used as the link text for the previous page</li>
<li><code>nxtlabel</code> &#8212; text/markup that is used as the link text for the next page</li>
</ul>
<h4>Caution: it&rsquo;s all backward</h4>
<p>Keep in mind that the default implementation of the <code>posts_nav_link()</code> template tag generates links that are essentially backward in order: the &ldquo;Previous&nbsp;Page&rdquo; navigation link points to <em>newer</em> posts, while the &ldquo;Next&nbsp;Page&rdquo; navigation link points to <em>older</em> posts. Exactly the <em>opposite</em> of what you would expect. Normally, previous posts are older and next posts are newer. To fix this backward behavior, we <em>could</em> simply customize the link text of either navigation link like so:</p>
<pre><code>&lt;?php posts_nav_link(' &amp;bull; ','&amp;laquo; Go forward in time','Go back in time &amp;raquo;'); ?&gt;</code></pre>
<p>This would then generate the following links on your archive pages:</p>
<p><a href="#">&laquo; Go forward in time</a> &bull; <a href="#">Go back in time &raquo;</a></p>
<p>This reflects reality to a greater degree, however the links still seem to appear out of order. It just seems counter-intuitive to have the &ldquo;Go&nbsp;forward&rdquo; link on the left side of the page and the &ldquo;Go&nbsp;back&rdquo; link on the right side of the page. It seems more intuitive and natural to assume the reverse, link left to go back and link right to go forward. Just like reading a book.</p>
<p>Fortunately, there are two ways to get the links to appear in the reverse (i.e., correct) order. Both methods require us to separate the navigational links so that they may be repositioned with the &ldquo;Go&nbsp;back&rdquo; link on the left side of the page and the &ldquo;Go&nbsp;forward&rdquo; link on the right. How we go about separating the links depends on the template tag that is used. There is a workaround the <code>posts_nav_link()</code> tag, but it is far easier to simply use the <code>previous_posts_link()</code> and <code>next_posts_link()</code> tags instead. We&rsquo;ll get to the easy way in the next section; for now, let&rsquo;s look at the workaround for the <code>posts_nav_link()</code> tag.</p>
<p>To separate the navigational links created by the <code>posts_nav_link()</code> tag, we use <em>two</em> instances of it on the page. In the first instance, we kill the &ldquo;Go&nbsp;forward&rdquo; link, and in the second instance we kill the &ldquo;Go&nbsp;back&rdquo; link. The result is the following code, which provides us with two properly identified navigation links that may be situated in the correct order:</p>
<pre><code>&lt;?php posts_nav_link('','','Go back in time &amp;raquo;'); ?&gt; &amp;bull; &lt;?php posts_nav_link('','&amp;laquo; Go forward in time',''); ?&gt;</code></pre>
<p>Perhaps the best way to understand this technique is to sequentially test each of the following navigation templates. Along the way, keep an eye on where the links are appearing on the page and where the links are pointing:</p>
<pre><code>&lt;?php posts_nav_link(); // default tag ?&gt;

&lt;?php posts_nav_link(' &amp;bull; ','&amp;laquo; Go forward in time','Go back in time &amp;raquo;'); // switched text ?&gt;

&lt;?php posts_nav_link('','','Go back in time &amp;raquo;'); ?&gt; &amp;bull; &lt;?php posts_nav_link('','&amp;laquo; Go forward in time',''); // switched text and reversed order ?&gt;</code></pre>
<p>&ldquo;And that&rsquo;s all I have to say about that,&rdquo; as the man once said. Let&rsquo;s break out of this nightmare and move on to some fresh examples.</p>
<h4>Examples</h4>
<p>Here are some examples to demonstrate the use and flexibility of the <code>posts_nav_link()</code> template tag:</p>
<p><strong>Your basic navigation</strong></p>
<p>Out of the box, the <code>posts_nav_link()</code> template tag displays your navigation links in reverse order, but you can still customize the text to make things a little clearer for your readers. One of the benefits of sticking with the default busted configuration is that you will never have to deal with empty markup elements that may appear when there are no newer or older posts to display. If squeaky-clean source code tickles your fancy, customize the default tag as needed and enjoy the results:</p>
<pre><code>&lt;?php posts_nav_link(' &amp;bull; ','&amp;laquo; Go forward in time','Go back in time &amp;raquo;'); ?&gt;</code></pre>
<p><strong>Keep &lsquo;em separated</strong></p>
<p>What if you want to display your &ldquo;Previous&rdquo; link on the left side of the page and the &rdquo;Next&rdquo; link on the right side of the page? We can acheive this effect by using two instances of the <code>posts_nav_link()</code> template tag (note that this configuration also resolves the &ldquo;backward-navigation&rdquo; issue discussed above):</p>
<pre><code>&lt;div class="left"&gt;
	&lt;?php posts_nav_link('','','&amp;laquo; Older Posts'); ?&gt;
&lt;/div&gt;
&lt;div class="right"&gt;
	&lt;?php posts_nav_link('','Newer Posts &amp;raquo;',''); ?&gt;
&lt;/div&gt;</code></pre>
<p><strong>Keep &lsquo;em separated with images</strong></p>
<p>Continuing with the previous example, let&rsquo;s keep the navigation links separate, but use images instead of text:</p>
<pre><code>&lt;div class="left"&gt;
	&lt;?php posts_nav_link('','','&lt;img src="images/older.png" /&gt;'); ?&gt;
&lt;/div&gt;
&lt;div class="right"&gt;
	&lt;?php posts_nav_link('','&lt;img src="images/newer.png" /&gt;',''); ?&gt;
&lt;/div&gt;</code></pre>
<p>Remember, you can use any text or markup you need in this template tag, so knock yourself out and conjure up something fresh.</p>
<h3 id="previouspostslink">previous_posts_link() and next_posts_link() &#8212; separate links for index, category, and archive pages</h3>
<p><small>[&nbsp;<a href="#navmenu" title="Return to post menu">&uarr;</a>&nbsp;]</small> <strong>The <code>previous_posts_link()</code> and <code>next_posts_link()</code> template tags provide separate &ldquo;next&rdquo; and &ldquo;previous&rdquo; navigational links for any type of paged archive view.</strong> Basically, this tag creates individual navigational links for anything other than single-post views. This includes index, category, archive, tag, and search pages. Best results when used either before or after the loop. Here is an example of these tags showing their two accepted parameters:</p>
<pre><code>&lt;?php previous_posts_link('label','max_pages'); ?&gt;

&lt;?php next_posts_link('label','max_pages'); ?&gt;</code></pre>
<p><strong>Default usage</strong></p>
<p>Here is the default usage of these template tags, followed by their corresponding output (bullet added for clarity):</p>
<p><code>&lt;?php previous_posts_link(); ?&gt; &amp;bull; &lt;?php next_posts_link(); ?&gt;</code></p>
<p><a href="#">&laquo; Previous Page</a> &bull; <a href="#">Next Page &raquo;</a></p>
<h4>Parameters</h4>
<p>Here are the accepted parameters for each of these template tags:</p>
<p><code>&lt;?php previous_posts_link('label','max_pages'); ?&gt;</code><br />
<code>&lt;?php next_posts_link('label','max_pages'); ?&gt;</code></p>
<ul>
<li><code>label</code> &#8212; alphanumeric string and/or markup used for the link text</li>
<li><code>max_pages</code> &#8212; an integer that limits the number of pages on which the link is displayed. The default value is &ldquo;<code>0</code>&rdquo; for all pages.</li>
</ul>
<h4>These are backward too</h4>
<p>Like the <code>posts_nav_link()</code> tag, the <code>previous_posts_link()</code> and <code>next_posts_link()</code> create links that do the <em>opposite</em> of what you expect them to do. The <code>previous_posts_link()</code> tag links to <em>newer</em> posts and the <code>next_posts_link()</code> tag links to <em>older</em> posts. Again, exactly the opposite of what you would expect.</p>
<p>Fortunately, these tags create links that are <em>spearate</em> from one another, enabling us to implement them according to conventional methods of navigation. Simply customize the link text such that it means the <em>opposite</em> of the tag&rsquo;s name and then switch the tags&rsquo; order in the source code. Let&rsquo;s look at a few examples to help clarify this technique.</p>
<h4>Examples</h4>
<p>Here are some examples to demonstrate the use and flexibility of the <code>previous_posts_link()</code> and <code>next_posts_link()</code> template tags:</p>
<p><strong>Customized link text</strong></p>
<p>To keep the navigation operating conventionally, simply switch the meaning of the link text to convey the <em>opposite</em> meaning of the template tag and then position the tags accordingly:</p>
<pre><code>&lt;?php next_posts_link('&amp;laquo; Previous posts'); ?&gt; &lt;?php previous_posts_link('Next posts &amp;raquo;'); ?&gt;</code></pre>
<p><strong>Default WordPress theme</strong></p>
<p>Even the default WordPress theme, Kubrick, uses this technique to display proper navigational links in the correct order and on opposite sides of the page: </p>
<pre><code>&lt;div class="navigation"&gt;
&lt;div class="alignleft"&gt;&lt;?php next_posts_link('&amp;laquo; Older Entries') ?&gt;&lt;/div&gt;
&lt;div class="alignright"&gt;&lt;?php previous_posts_link('Newer Entries &amp;raquo;') ?&gt;&lt;/div&gt;
&lt;/div&gt;</code></pre>
<p>Thus, we see how to use the <code>previous_posts_link()</code> and <code>next_posts_link()</code> template tags to easily display your post-archive navigational links appear in the conventional, intiuitive, and correct order. Problem solved.</p>
<p><strong>Keep &lsquo;em separated with images (best method)</strong></p>
<p>Here&rsquo;s the <em>proper</em> way to create navigational links using images instead of text:</p>
<pre><code>&lt;div class="left"&gt;
	&lt;?php next_posts_link('&lt;img src="images/older.png" /&gt;'); ?&gt;
&lt;/div&gt;
&lt;div class="right"&gt;
	&lt;?php previous_posts_link('&lt;img src="images/newer.png" /&gt;'); ?&gt;
&lt;/div&gt;</code></pre>
<p>And as before, you may use any text or markup with this template tag, making it possible to integrate into just about any design.</p>
<h3 id="previouspostlink">previous_post_link() and next_post_link() &#8212; separate links for single-view post navigation</h3>
<p><small>[&nbsp;<a href="#navmenu" title="Return to post menu">&uarr;</a>&nbsp;]</small> <strong>The <code>previous_post_link()</code> and <code>next_post_link()</code> template tags provide separate links to the previous post and next post, respectively</strong>. In doing so, these tags enable visitors to navigate chronologically through your single post permalink pages. These tags may be used anywhere on the <code>sinlge.php</code> template page, either in or out of the loop. Here is an example of these tags showing their four accepted parameters:</p>
<pre><code>&lt;?php next_post_link('format','link','in_same_cat','excluded_categories'); ?&gt;

&lt;?php previous_post_link('format','link','in_same_cat','excluded_categories'); ?&gt;</code></pre>
<h4>Default usage</h4>
<p>Unlike other navigational tags, <code>next_post_link()</code> and <code>previous_post_link()</code> eliminate navigational confusion and ambiguity by using actual post titles for their link text. Here are some examples of the default usage of these tags, followed by their corresponding output (bullet added for clarity):</p>
<p><code>&lt;?php previous_post_link(); ?&gt; &amp;bull; &lt;?php next_post_link(); ?&gt;</code></p>
<p>&laquo; <a href="#">Previous Post Title</a> &bull; <a href="#">Next Post Title</a> &raquo;</p>
<h4>Parameters</h4>
<p>Here are the accepted parameters for each of these template tags:</p>
<p><code>&lt;?php next_post_link('format','link','in_same_cat','excluded_categories'); ?&gt;</code><br />
<code>&lt;?php previous_post_link('format','link','in_same_cat','excluded_categories'); ?&gt;</code></p>
<ul>
<li><code>format</code> &#8212; format string for the link. Here you specify the text/HTML to appear before and after the link. The link itself is specified according to the <code>link</code> parameter by including the <code>%link</code> variable in the format string. For example, a <code>format</code> value of &ldquo;<code>Next: %link &amp;raquo;</code>&rdquo; will result in the following output: &ldquo;Next <a href="#">The Post Title</a>&nbsp;&raquo;&rdquo;</li>
<li><code>link</code> &#8212; link text to display. Anything specified here will be used as the <code>%link</code> variable in the <code>format</code> parameter (see previous). By default, this parameter returns the post title via the <code>%title</code> variable. For example, a <code>link</code> parameter with a value of &ldquo;<code>[[%title]]</code>&rdquo; will result in the following output: &ldquo;<a href="#">[[The Post Title]]</a>&nbsp;&raquo;&rdquo;</li>
<li><code>in_same_cat</code> &#8212; boolean value indicating whether or not the next or previous post must be in the same category as the current post. If set to <code>TRUE</code>, only posts from the current category will be displayed. Options are: <code>TRUE</code> or <code>FALSE</code> (default).</li>
<li><code>excluded_categories</code> &#8212; string indicating any collection of category IDs from which the next post should not be listed. In all versions of WordPress except 2.2, multiple categories are separated via &ldquo;<code> and </code>&rdquo;, for example: &lsquo;<code>1 and 3 and 7</code>&rsquo; Oddly enough, in WordPress version 2.2, a comma is used to concatenate multiple excluded categories, for example: &lsquo;<code>1, 3, 7</code>&rsquo;</li>
</ul>
<h4>Examples</h4>
<p>Here are some examples to demonstrate the use and flexibility of the <code>previous_post_link()</code> and <code>next_post_link()</code> template tags:</p>
<p><strong>Custom title with link description before/after post link</strong></p>
<p>Here we are displaying custom link titles with the text &ldquo;Previous&nbsp;post:&nbsp;&rdquo; and &ldquo;Next&nbsp;post:&nbsp;&rdquo; appearing before the previous and next post, respectively:</p>
<pre><code>&lt;?php previous_post_link('Previous post: %link', '[ %title ]'); ?&gt; &amp;bull; 
&lt;?php next_post_link('Next post: %link', '[ %title ]'); ?&gt;</code></pre>
<p><strong>Links to posts within the same category, excluding one</strong></p>
<p>Here is how to display next and previous post links only to posts that are in the same category as the current post unless the current post is in category <code>7</code>. Any number of categories may be excluded from this &ldquo;same-category&rdquo; navigation by using &ldquo;&nbsp;<code>and</code>&nbsp;&rdquo; as a delimiter:</p>
<pre><code>&lt;?php previous_post_link('%link', '%title', TRUE, '7'); ?&gt; &amp;bull; 
&lt;?php next_post_link('%link', '%title', TRUE, '7'); ?&gt;</code></pre>
<p><strong>Display images as links</strong></p>
<p>Instead of using text for the next/previous-post links, we can use a custom set of images:</p>
<pre><code>&lt;?php previous_post_link('%link', '&lt;img src="images/older.png" /&gt;', TRUE, '7'); ?&gt; &amp;bull; 
&lt;?php next_post_link('%link', '&lt;img src="images/newer.png" /&gt;', TRUE, '7'); ?&gt;</code></pre>
<p>Indeed, there is much that may done with this set of template tags. If you know of any juicy tricks not covered here, share them in the comments!</p>
<h3>One for the road</h3>
<p>Every now and then, you may encounter an older WordPress theme that includes the following navigation tags:</p>
<pre><code>&lt;?php previous_post(); ?&gt;
&lt;?php next_post(); ?&gt;</code></pre>
<p>For the record, these are the now-deprecated predecessors of the previously discussed <code>previous_post_link()</code> and <code>next_post_link()</code> template tags. The deprecated versions provide navigation of single permalink post views and use essentially the same parameters as the current tags. Now you know!&nbsp;:)</p>
<h3>References</h3>
<ul>
<li><a href="http://codex.wordpress.org/Template_Tags/posts_nav_link" title="WordPress Codex: posts_nav_link()">posts_nav_link()</a></li>
<li><a href="http://codex.wordpress.org/Template_Tags/previous_posts_link" title="WordPress Codex: previous_posts_link()">previous_posts_link()</a></li>
<li><a href="http://codex.wordpress.org/Template_Tags/next_posts_link" title="WordPress Codex: next_posts_link()">next_posts_link()</a></li>
<li><a href="http://codex.wordpress.org/Template_Tags/previous_post_link" title="WordPress Codex: previous_post_link()">previous_post_link()</a></li>
<li><a href="http://codex.wordpress.org/Template_Tags/next_post_link" title="WordPress Codex: next_post_link()">next_post_link()</a></li>
</ul>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/08/wordpress-page-navigation/">Permalink</a> | <a href="http://digwp.com/2009/08/wordpress-page-navigation/#comments">30 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/08/wordpress-page-navigation/&title=Definitive Guide to WordPress Page Navigation">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/navigation/" rel="tag">navigation</a>, <a href="http://digwp.com/tag/tags/" rel="tag">tags</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/08/wordpress-page-navigation/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
	</channel>
</rss>

