Archive page problems with WordPress

Posted on June 7, 2009 by Phil   no comments yet

While redesigning my site recently I wanted to display a list of just the titles and dates for each article on the archives page. This was easy enough but as I have my settings set to show 5 posts per page I was getting just that a list of the latest 5 and a link to show more which looked a bit crap to say the least. Looking through the codex for a quick fix didn’t bring up anything obvious so I had to somehow create a new query to display the number of posts I wanted from each category. However I needed to also pass the new query the category name else I would just end up with posts from every category. Here’s how I did it…

At the top of my Archive.php file I was already pulling the category title to display as a page heading so I simply used the single_cat_title() function to store the category name in a variable to use in my new query.

<h2><?php $current_cat = single_cat_title('', false); echo $current_cat; ?> Archive</h2>

The first argument is used to prefix a given string to the returned title which isn’t used when setting the display, second argument, to false so I just left it empty and with display set to false the returned value is stored in $current_cat, the variable I will use in the new query.

Setting the new query

So next I created my query to pull more than the default number of posts from the $current_cat, which in my case was 30.

<?php query_posts('category_name=' . $current_cat . '&showposts=30'); ?>

That’s it, simple but works perfectly!

Related Posts

No related posts.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>