WordPress: Display different number of pages on Archives

Wordpress Different Pages on Archives

I’m working on a little project to be released in the next weeks. Because of this I’ve been improving my WordPress knowledge quite a bit. The website will be a News / Articles type of site. I intended to have 6 posts on the front page, something you can control under the WordPress Options in Settings > Reading.

I wanted to make the archives behave differently from the Index, that is, display a different amount of posts. I stilled them differently so it only made sense to have a larger amount of posts when you clicked a category, tag, etcetera. This seemed a very easy task to achieve, I though of using query_posts() with the argument ‘posts_per_page = 10′, like: query_posts(‘posts_per_page=10′);. It worked, I had now 10 posts per page when displaying the archives, but it was working badly, it displayed all posts not just the desired category, tag, etc. As usual I’ve Google to work to find a solution. I did find a lot of people with similar problems, hell I even found WordPress Plugins for the Query_Post. What I didn’t remember to do was RTFM. Don’t get me wrong, I use the WordPress Codex all the time, but in this case I didn’t think it would cover such a specific “problem”. It did, you can find the solution in the Codex but I’ll leave it here for future reference if anyone has the same problem as I did.

Basically what we want to do is do a different query, using query_posts() but still retain the ability from the loop to get the parameters from the URL (the category, tag…). What we do is use the $query_string global variable in the call to query_posts().

In my example:

< ?php
 global $query_string;
 query_posts('$query_string . posts_per_page=8');
 while (have_posts()) : the_post(); ?>

(... the loop ...)

With global $query_string your archives will work the intended way and you can still do a different query with query_posts() to display a different amount of posts. Hope this quick tip can help you in your future WordPress endeavors.

PS – And yes, the post image is the preview of the project I’m working on.

 

Copyright © 2008 - 2010 Paintbits. All rights reserved. Hosted by WebHS.