Adding category index to wordpress.

I wanted to change wordpress so that when people click on my categorys they get a nice index page with all the possible posts..

There were a few ways you could achieve this, but I plumped for modifiying archive.php, mostly because I like playing.

To do this, you need to edit wp-content/themes/yourthemename/archive.php.

In archive.php you will see a number of lines, find the one that has the following:

<?php if (have_posts()) : ?>

For me in my theme it was under a load of if elseif loops setting up the archive type.

Once you have found that part, insert the following ABOVE that line.

    $temp_category = single_cat_title(”,false);
  //  if (!empty($temp_category)){
    // give index
   // ?>
    <h1><?php single_cat_title(); ?></h1>
    <p><?php echo(category_description(the_category_ID(false))); ?></p>
<?php query_posts(‘posts_per_page=-1&cat=’.$cat); ?>
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <a href=”<?php the_permalink() ?>”><?php the_title(); ?></a><br><br><?php echo $the_post; ?></br>
    <?php endwhile; else: ?>
    <p><?php _e(‘Sorry, no posts matched your criteria.’); ?></p>
   <?php endif; ?>
//   }else{ // give details or single post

 <?php query_posts(‘posts_per_page=3&cat=’.$cat); ?>


The line <?php query_posts(‘posts_per_page=-1&cat=’.$cat); ?> basically says dont limit the number of post headings that is displayed, I have it set to -1 which means all, but if you wanted you could limit this to whatever number you fancy – 10, 20 etc..

The line at the bottom – <?php query_posts(‘posts_per_page=3&cat=’.$cat); ?> says show the first 3 articles below for the category you are in, again you might want to increase or decrease this number.

You will also need to close things up at the end, parentheses and if statements.

Its hard to post this as  each template is different, so drop me a comment if you have trouble.

You will know if you haven’t closed as you will get an unexpected error when you try to view your page.

You can follow any responses to this entry through the RSS 2.0 feed.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This blog is kept spam free by WP-SpamFree.