2
Jun
Quick Tip #2 - Changing the post order on Wordpress’s category/archive pages
Posted by Benjamin Reid in Quick Tips, WordPress
Posted by Benjamin Reid in Quick Tips, WordPress
I’ve had this problem (changing the order on those darn categories pages) from day one since working with WordPress and it really bugged me when I’d had to resort to plugins to solve the problem. Basically, when you select different categories in WordPress the post order and category is set in a query which you can’t see. It might look something like this -
<?php query_posts('cat=3&order=desc'); ?>
When I’ve wanted to change the order to ascending I’ve tried to add the following into the archive.php file above the loop.
<?php query_posts('order=asc'); ?>
This then messed up the categories selection and the post order on the page. Then I found $query_string which you can add to the start of the query_posts() function which holds the original query generated by WordPress!
You can then carry on with your custom query, but don’t forget the & at the start of your query as you are joining it on to WordPress’s post order query.
<?php query_posts($query_string . '&order=asc'); ?>
Here’s an example of a slimmed down loop you can use.
<?php query_posts($query_string . '&order=asc'); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <?php endwhile; ?> <div class="navigation"> <div class="alignleft"><?php next_posts_link('« Older Entries') ?></div> <div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div> </div> <?php else : get_search_form(); endif; ?>
My name is Benjamin Reid, I live in the UK and keep the magic locked into this site.
This entry was posted on Tuesday, June 2nd, 2009 at 11:59 am and is filed under Quick Tips, WordPress & tagged with PHP, WordPress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
wp-popular.com » Blog Archive » wordpress categories post order | Nouveller wrote on November 1, 2010
[...] the original post: wordpress categories post order | Nouveller Tags: events, order, [...]
effivaZex wrote on October 4, 2011
bb.txt open error