Sorting a Wordpress Category Page

by @jehiah on 2005-12-14 08:16
Filed under: 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

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>
Subscribe via RSS ı Email
© 2017 - Jehiah Czebotar