Display different number of Posts

When using WordPress, I often see the need to display a different number of Posts in different contexts. Maybe I just want to display the five newest on the frontpage, but show 50 (with just the headlines) when browsing Archives, etc. WordPress doesn’t let you do this – at least not from the Dashboard – in Reader Settings there’s only one field that lets you set the number of Posts, which is used all-over (defaults to 10).

But there are ways around this. The plugin Custom Query String Reloaded lets you set a different numbers of Posts for everthing from the frontpage, to archives and categories.

You don’t need to use a plugin, however, if you know your way around your Theme-files. To change the number of displayed Posts, you simply need this little code snippet:

<?php query_posts('posts_per_page=5'); ?>

Insert it above the Loop in your template-files, and change the number to the number of Posts you want to display. Remember to use Conditional Tags if you want different results on different pages. For example, if you want to show five Posts on the frontpage only, you add this code above the Loop in your index.php:

<?php if(is_home()) query_posts('posts_per_page=5'); ?>

The Loop (usually) starts with the line <?php if (have_posts()) : ?>, so insert your code above this.

This can be used in practically every template-file that deals with multiple Posts; index.php, archives.php, category.php, and so forth.

3 thoughts on “Display different number of Posts

  1. Pingback: Stian Andreassen » Blog Archive » Sort Posts by date or title

  2. Keay

    I find this method doesn’t work correctly…When set, yes it does display the number of posts according to what you have put BUT when you click on “next” page or “previous” page, the posts doesn’t change.

    Strange…

    Reply
  3. Pingback: Razee » Blog Archive » How to Display Different Numbers of 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>