Plugin Sale! Save 15% on pro plugins with discount code: FALL2020
Web Dev + WordPress + Security
eBook: WordPress Themes In Depth
WP Themes In Depth: Deep dive into WP theme development.
Related Posts

Reversing WordPress Page Navigation Order

This article explains how to reverse the order of WordPress archive navigation links. For more information about WP’s navigation tags, check out this post.

Step 1

To reverse the default (backward) order of page navigation in WordPress, open the file template-functions-links.php (in WP 2.0) or link-template.php (in WP 2.1/2.2) and scroll down to the last function, posts_nav_link()1. Next, find the last if statement and switch the order of the following two lines:

previous_posts_link($prelabel);
   [ ignore this line ]
next_posts_link($nxtlabel, $max_page);

Step 2

Next, within the first line of the same function (beginning with function posts_nav_link), switch the order of the variables $prelabel='« Previous Page' and $nxtlabel='Next Page »'.

Step 3

Finally, within the loop, make sure you are calling the function with the usual parameters in place. Something like:

<?php posts_nav_link(' | ','&laquo; Previous Entries','Next Entries &raquo;') ?>

That’s it! Your post navigation links (for index and category views) should now operate according to the following principle: left links point to older posts and right links post to newer posts.

Footnotes

1 Not to be confused with previous_post_link() and next_post_link(), which provide navigational links to older individual posts and newer individual posts, respectively. Oddly enough, in the case of post-by-post navigation, WordPress gets it right: left links point to older posts and right links post to newer posts; however, this is not the case with page-by-page navigation, in which WordPress adopts the reverse strategy. For more information, check out WordPress Post Navigation Redux (New Tags!).

Jeff Starr
About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
USP Pro: Unlimited front-end forms for user-submitted posts and more.

6 responses to “Reversing WordPress Page Navigation Order”

  1. Thanks a lot, looks like it works at my homepage.

  2. Jeff Starr

    You are quite welcome!

  3. Is there an easy way to reverse the entire collection of posts so that the oldest post comes first?

  4. Jeff Starr

    DLa,

    As a matter of fact, adding the parameter, "&order=ASC" to the end of the query string accomplishes such a trick. Simply include the following line of PHP immediately before the WordPress Loop:

    <?php query_posts($query_string . "&order=ASC"); ?>

  5. How would I get my posts to display in a random order? I can find info anywhere and this is about as close as I’ve gotted:) and help would be appreciated

  6. Sorry for the horribly typed question. I was in a huge rush. You seem to have the php. brain that can answer my question.

    How do you ‘shuffle’ posts that are in a category. So that each time the category is viewed they are in a new order??

    Thanks in advance

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
The Tao of WordPress: Become your own WordPress guru.
Thoughts
Air finally clearing here in WA. Feeling grateful to breathe again. #oxygenmatters
Past week here in WA state has been hellish. So much smoke, like living in a chimney.
Now in September, I’m where I wanted to be in March.
Spent some time updating my article on unsafe characters, once again current with latest IETF specification.
Just realized that “Neo” is an anagram for “One”. As in, “he is the One” (The Matrix).
To get VLC app to load all songs (including subfolders), go to Preferences ▸ Show All ▸ Playlist ▸ Subdirectory behavior ▸ Expand.
Switching from PhotoShop to Affinity Photo is one of the most liberating work-related things I've done in 20 years.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.