Ascending Post Order for Category Pages in WordPress 2012

Took me a while to get this working. Requires a hosted version of WordPress; I don’t think this sort of thing is possible if you’re using a free WordPress.

  1. Edit the main wordpress query to set ascending order on category pages
    1. Go to Appearance > Editor and open functions.php (“Theme Functions”)
    2. Add the following code after the <?php
    3. Click Update file
  2. View a category page – your posts should now be in ascending order
  3. Check the home page – the posts should still be in descending order
  4. On the category page you probably have an “older posts” button at the bottom of the page, which should really now read “newer posts”. An easy way to avoid this problem is to use infinite scrolling
    1. Go to Settings > Reading
    2. Check “scroll infinitely”

Here’s the code you need

 


add_action( 'pre_get_posts', 'my_query' );

function my_query( $wp_query ){
if(is_admin() || !$wp_query->is_main_query()){
return;
}

if ( is_category() ){
$wp_query->set( 'order', 'ASC' );
}
}

Advertisements
Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: