One thing I’ve been looking at lately is the ability to create a customized category page that will display more posts per page than the current WordPress settings.
To do that all I did was created a file called category.php. Since WordPress looks to the theme’s directory for particular files it uses them in an hierarchy sort of order. If you’re looking at a single page/post, it will use the single.php file. If you are looking at a single page (such as a Contact, or an About page) it will use page.php.
With Archives and Categories it will first look for either archive.php or category.php, if those are not present it will default to the index.php. In the case of www.tyleringram.com the default setting to display posts per page is set to 4. In terms of category posts, I wanted it to display at most 10. I would have to somehow override this and this is where creating a category.php file comes in handy.
I first copied the index.php file I currently used for the theme and renamed it to category.php. I then began to tweak the settings to work with Categories.
In order to override the default posts-per-page setting I had to create a slightly customized query to introduce to the WordPress Loop. This is how I did it:
// Let's retrieve 10 posts per page and ensure that paginating works $page = get_query_var('paged'); $cat = get_category($cat); query_posts('cat='. $cat->cat_ID.'&posts_per_page=10&paged='. $page);?>
Seems pretty straight forward right? I retrieved the value of ‘paged’ which is used for pagination when the user navigates past the first 10 posts on the first page in a particular category.
I then retrieve the category’s values which will go into making a custom query to retrieve the proper posts within that query. You can also see that I have set the ‘posts_per_page’ value to 10. This will display 10 posts within a particular category on each page.
Those 3 lines (the first line is a comment in PHP) will now display 10 posts within a category on each page. Just make sure the lines are above the WordPress Loop line that looks like this:
<?php while (have_posts()) : the_post(); ?>
Pretty easy right?
I also tweaked one of the custom functions I created to display thumbnails of the first photo within a post. I made them 150 pixels wide instead of 500 pixels wide as with the index.php file.