How to limit number of posts on your WordPress homepage.


If you have been blogging regularly for more than few months, chances are you have quite a few posts on your blog. That is a good thing. As a blogger you are supposed to blog regularly to provide useful and relevant information to your blog readers. But unless you dictate how your homepage is displayed WordPress will display several posts (or excerpts of posts) on the homepage. Usually you don’t want to display all your posts on the front page. So how do you tell WordPress to display only a certain number of posts on the front page? The answer is simple.

You can certainly use a plugin to limit the number of posts on the front page, but if you are comfortable with editing a PHP file then you can accomplish this task yourself without installing a plugin.


All you need to do is to add following code before the loop statement in the index.php file of your theme.


<?php
//Set limit of posts on the front page to 8. 
query_posts('posts_per_page=8');
? >

A word of advice: Make a backup of your index.php file before editing, just in case you need to revert to the original file.

Open index.php in a text editor of your choice or in WordPress. Search for the line


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

Replace this line with


<?php query_posts('posts_per_page=8');
 if(have_posts()) :  php while(have_posts()) : the_post(); ?>

After end of the loop, you should have a line with


<?php endif; ?>

After this line add following line to reset the query.

<?php wp_reset_query();?>

Now on your homepage you will have only 8 posts (or excerpts).
I randomly used '8', you can put any number you desire.

Enjoy!