How to Customize number of blog posts on first page?

Under Reading Settings, I can specify how many blog posts to display on a page. However, I would like to customize this a bit. For example, I want to only display 2 posts on the “/” page, but 3 posts on “/page/2”, “/page/3”, etc. Any way to customize the homepage to display one less post than the other pages?

Related posts

Leave a Reply

3 comments

  1. There is no option to change the number of posts per page, archive, tag or category from the admin but you can use query_posts() posts_per_page to set the number.
    for example in your theme’s index.php file or if you are using a static page as your home_page the page.php file add this snippet of code above the loop

    if(is_home() || is_front_page()){
        global $query_string;
        parse_str( $query_string, $args );
        $args['posts_per_page'] = 2;
        query_posts($args);
    }
    

    what it does is check to see if you are on the home page or front page and if you are then it changes the number of posts to show.

    another example is to show different number of posts for different categories:

    global $query_string;
    parse_str( $query_string, $args );
    if(is_category('cats')){
        $args['posts_per_page'] = 7;
        query_posts($args);
    }elseif(is_category('dogs')){
        $args['posts_per_page'] = 4;
        query_posts($args); 
    }else{
        $args['posts_per_page'] = 10;
        query_posts($args);     
    }
    

    here we use the conditional tag is_category() to check which category we are in based on that we set the number of posts to display.
    For cats we set 7, for dogs we set 4 and if its just random category we display 10.

  2. http://coffee2code.com/wp-plugins/custom-post-limits/

    By default, WordPress provides a single configuration option to control how many posts should be listed on your blog. This value applies for the front page listing, archive listings, category listings, tag listings, and search results. Custom Post Limits allows you to override that value for each of those different sections.

    Specifically, this plugin allows you to define limits for:

    • Authors archives (the archive listing of posts for any author)
    • Author archives (the archive listing of posts for any specific author)
    • Categories archives (the archive listings of posts for any category)
    • Category archive (the archive listings of posts for any specific category)
    • Date-based archives (the archive listings of posts for any date)
    • Day archives (the archive listings of posts for any day)
    • Front page (the listing of posts on the front page of the blog)
    • Month archives (the archive listings of posts for any month)
    • Search results (the listing of search results)
    • Tags archives (the archive listings of posts for any tag)
    • Tag archive (the archive listings of posts for any specific tag)
    • Year archives (the archive listings of posts for any year)
  3. I found this thread although my requirements were a bit different from the OP’s. I needed to have 16 posts on the first page and subsequently 15 posts on the next pages. I got to this solution thanks to @Jan Fabry’s thread ‘How to show a single post on the front page but have normal paging‘.

    I’m posting the solution here since it seems appropriate to the title of the thread and also while Bainternet’s solution works, the WP codex discourages using the query_posts() function. Furthermore, there will be a duplicate post on page 2 since we didn’t offset the subsequent posts (WordPress thinks there are only 15 posts on the frontpage even though there are 16)

    The offset is calculated from page 2 onwards. So if you are on page 2 and the posts per page in the dashboard settings is 15, the offset is ((2-2) * 15) + 16 = 16 on page 3 it is 31.

    There’s also and extra check that the query is the main query as outlined in the codex in the pre_get_posts hook.

    function set_offset_on_front_page($query)
    {
      if (!is_admin() && $query->is_main_query()) {
        if (is_category() && !is_paged()) {
          // +1 because we want to add the Photo Gallery (former chapter guides) to the query and still maintain the 3 per row layout
          $query->query_vars['posts_per_page'] = get_option('posts_per_page') + 1;
        }
        if (is_category() && is_paged()) {
          $posts_per_page = isset($query->query_vars['posts_per_page']) ? $query->query_vars['posts_per_page'] : get_option('posts_per_page');
          $query->query_vars['offset'] = (($query->query_vars['paged'] - 2) * $posts_per_page) + $posts_per_page + 1;
        }
      }
    }
    add_action('pre_get_posts', 'set_offset_on_front_page');