Sorting a Wordpress Category Page

posted December 14th 2005 at 0816 EST in All, PHP

This thread prompted me to post an updated to an old post on Wordpress Category Page Hacks as the old page was for wordpress 1.2, and I came up with a much simpler solution for wordpress 1.5. Below are instructions for customized sorting of Wordpress 1.5 category pages.

What I wanted to do, was to display my category pages in a single page format (ie: not 10 items per page), and to allow users the option to alternate between sorting by title (default) and date.

Previously I had hacked the php index page pretty badly, but this time around I use a simple category.php file, and a few small additions to my .htaccess file.

First in the .htaccss I modified the default category mapping line to sort by title, and show all the posts (or atleast 500 of them) on the same page


RewriteRule ^archive/category/?(.*) /index.php?category_name=$1&orderby=title&order=asc&posts_per_page=500 [QSA]
 

Next I added one more lines which allow two additional urls to select between sorting by title and sorting by date.

The Url's I wanted to use are in the following format

http://jehiah.com/archive/category/javascript/date/desc
http://jehiah.com/archive/category/javascript/title/asc

So the following .htaccess line takes the values for sort column and sort direction out of the url and passes them on to the rewrite.


RewriteRule ^archive/category/(.*)/(.+)/(.+) /index.php?category_name=$1&orderby=$2&order=$3&posts_per_page=500 [QSA]
 

Lastly, the following line in my category.php template file gives the proper links to each version of my category page.


<p>Sort by <a href="/archive/category/<?php echo str_replace("/","",$_GET['category_name']); ?>/title/asc">Title</a> or\
 <a href="/archive/category/<?php echo str_replace("/","",$_GET['category_name']); ?>/date/desc">Date</a></p>
 

2 Responses

  1. #1 Mark
    4 years, 2 months ago

    Cool.This seems like a nice solution.

    Do you know whether it would be possible to do the same thing but order not by title but by a custom field value? (I know it’s an odd request, but I have a good reason…)

  2. #2 Rk
    4 years, 2 months ago

    Good one.
    What if I have nested categories ?