Mar
20
2010

 

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:
    [c]
    get_query_var('cat');
    [/c]
    
  • To alter the number of posts being displayed, set the posts_per_page variable in the global query_string, like below:
    [c]
    global $query_string;
    query_posts($query_string . "&posts_per_page=".$posts_per_page);
    [/c]

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

    [c]
    <?php if (have_posts()) : while(have_posts()) : the_post(); ?>
    [/c]

    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:
    [c]
    global $query_string;
    query_posts($query_string . "&orderby=title&order=DESC");
    [/c]

    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.

    [c]
    <?php if (have_posts()) : while(have_posts()) : the_post(); ?>
    [/c]

Comments are closed.