WordPress – Differing Number of Posts on Main Page and Archive/Search Pages

Disclaimer

The instructions/steps given below worked for me running CentOS 4.0. It may very well work for you on other linux distributions, Red Hat-like or otherwise. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, sgowtham.com, nor its author is responsible for any/all damage – intellectual or otherwise.


What Is The Need?

It might, at some point in a WordPress powered blog, become necessary to a show a set number of posts show up in the main page (index.php of a given theme) while show all posts in archives/search page (archives.php / search.php of a given theme). Thanks to my dear friend, Amy – if not for her, I probably would have never learned about this hack.


Main Page

Within a given WordPress theme, one can find index.php and in the early parts of this file, one can find this line:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

Add the following line just before the above line. get_option('posts_per_page') makes sure that only the set number of posts (as set in WordPress Admin Interface) are displayed in the main page:

<?php $posts=query_posts($query_string . '&showposts=' . get_option('posts_per_page')); ?>


Archive Page

Within the same theme folder, one can also find archive.php and in the early parts of this file, one can find this line:

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

Add the following line just before the above line. showposts=-1 makes sure that all posts are displayed in the archive page:

<?php $posts=query_posts($query_string . '&showposts=-1'); ?>


Search Page

Within the same theme folder, one can also (sometimes) find search.php and in the early parts of this file, one can find this line:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

Add the following line just before the above line. showposts=-1 makes sure that all posts are displayed in the search page:

<?php $posts=query_posts($query_string . '&showposts=-1'); ?>


Screenshots

With these editions, the main and archives/search pages will look as follows (thanks to Amy again; I stole screenshots from her website):



Specific number of posts on index.php

Specific number of posts in main page




All posts on archive/search pages

All posts in archive/search page