I can’t believe I don’t have blog post about this. But I went looking for it today to make some adjustments to my new boating blog and there are absolutely no instances of me blogging about it. So without further ado…

Reversing Blog Post Order

One of the things you often come across is a blog or online journal that traces someone’s journey. For me lately this has been blogs of boaters or people who have set out on a grand adventure to sail away from their land-based lives. Naturally you want to read these from the beginning to the end. I was/am astounded the number of blogs that don’t allow this. But its not really surprising given the hoops one has to jump through in order to change how a blog is designed to be used. This is my solution for WordPress but there must be one out there for Blogger or other common blogs.

Since this blog uses a theme I designed myself from ground up, I figured it should be pretty easy too solve the issue and this turned out to be pretty true. For a couple of years now people have been able to read my Trip Reports in chronological order simply by reading them by category. With a bit of file editing, this was a snap to do.

Duplicate the category.php page ( /wp-content/themes/[your theme])and rename it category-(insert category ID).php. For example all the posts in my Broughton trip are in category with the ID of 89, so I created category-89.php file in my Theme’s folder. WordPress automatically uses that file when asked to display all the posts in the Broughtons category. Finding the category ID might take a bit of trial and error.

The Loop

Most category pages will have a thing called The Loop, which essentially calls all the entries from the database while certain conditions are true. WordPress’s default is to call the posts in descending order. This is the basic loop:

<?php 
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); 
        //
        // Post Content here
        //
    } // end while
} // end if
?>

To change the order I edit that file’s php adding <?php query_posts($query_string.”&orderby=date&order=ASC”); ?> after the initial if ( have_posts() ) call. That way the loop calls the individual posts in ascending order. Since this is done in the custom category php file it only effects that specific category. Other categories will still display posts in the traditional descending order.

<?php if ( have_posts() ) : ?><?php query_posts($query_string."&orderby=date&order=ASC"); ?>

Good Code

Now I am admittedly not much of a coder and since I wanted to have all Trip Reports viewable in ascending order, this method has resulted in a score of category-x.php files littering up my theme. But it worked for me and I haven’t been motivated to change it.

But now that I have started to work on a boat blog and I wanted to use someone else’s more elegant theme, my simple solution didn’t work. For one thing the theme I chose didn’t have a loop in the category page, but simply a Function that called the loop based on a parameter of ‘category’. Then when I investigated the function.php file, I found this theme doesn’t use a simple and easily parsed Loop and, since it’s a Function not a direct php call, the code is subtly different.

The theme’s function used one main loop to do calls for all the various scenarios: list by category, list by author, list on front page, list on blog page, etc. What I needed to do was find the appropriate IF statement and embed query_posts (array( ‘orderby’ => ‘date’, ‘order’ => ‘ASC’)); within the appropriate <?php statement. A bit of trial and error resulted in a limited success. Always remember to have backups you can revert to! Now if you select anything but the default post view i.e. a category page, all posts will now be displayed in ascending order.

If I want to display only certain categories or posts by certain authors I am going to have to figure out both how and where to insert a custom IF statement in the Function or easier yet add a new Function by duplicating the existing one, adding the change and renaming it. Then I can call it from my typical custom Category page. I haven’t decided which is a more elegant yet simple solution…

 

Postscript

So after all that I still updated the Fruitful theme and still lost my custom code, and this babling above was still not enough to make it an easy fix. Sigh.

So what I needed to do was insert the code below in the functions.php file at around line 1670.

/* CUSTOM REVERSE ORDER ON CATEGORIES ETC */
query_posts (array( 'orderby' => 'date', 'order' => 'ASC'));
/*END CUSTOM*/

This is the section that starts if ( ! function_exists( ‘fruitful_get_content_with_custom_sidebar’ ) ) and comes right after the

< ?php if ( have_posts() ) : ?>

somewhere around line 1735

< ?php 
/* CUSTOM REVERSE ORDER ON CATEGORIES ETC */ query_posts (array( 'orderby' => 'date', 'order' => 'ASC'));
/*END CUSTOM*/
            if ( is_category() )