HTML
WordPress: Customizing Category Pages
March 20, 2010
0

I am using WordPress 2.9 for this example:

  • Open Appearance -> Editor. Locate the php file that is being used to display categories.  It’s either Archieves.php or Categories.php.  You can echo something and open up a category page in the browser to see which one is being used. You can also edit the file in a PHP editor (the files are within your theme folder).
  • To get the category id (the digit id), use:
    get_query_var('cat');
    
  • To alter the number of posts being displayed, set the posts_per_page variable in the global query_string, like below:
    global $query_string;
    query_posts($query_string . "&posts_per_page=".$posts_per_page);
    

    This must be done before The Loop. What is The Loop? It’s this line that iterates every post returned by the query:

    <?php if (have_posts()) : while(have_posts()) : the_post(); ?>
    

    See http://codex.wordpress.org/The_Loop for more detail.

  • To display category name, use $category_name (the result is all lower case) or single_cat_title().
  • To alter the sorting order of posts being displayed, set the orderby and order variables in the global query_string, like below:
    global $query_string;
    query_posts($query_string . "&orderby=title&order=DESC");
    

    order can be ASC or DESC.
    orderby can be title, author, date, modified, id — this is not a complete list, check WordPress docs for the full list.

    Anytime we are altering the query, it must be done before The Loop also.

    <?php if (have_posts()) : while(have_posts()) : the_post(); ?>