Changing post count on home page in WordPress template


Some professional WordPress websites usually have custom post roll layout and different post count on homepage. So this means when you go to second page of post roll you should’t see repeating items as a consequence of another post count than set in ‘Blog pages show at most’ setting in Reading section of Settings in WP-admin.

Solving this problem is a simple code you need to add before wordpress loop in your index.php or home.php:

if ( is_home() ) {
    $posts_home = 9; //Set custom post number to show on home
    if( !is_paged() ){
	    query_posts("posts_per_page=$posts_home");
    } else {
	    if ( get_query_var('paged') != 2 )
    		$offsetting = $posts_home + ( ( get_query_var('paged') - 2 ) * get_query_var('posts_per_page') );
    	else
    		$offsetting = $posts_home;
	    query_posts($query_string . "&offset=$offsetting");
	}
}

Here $posts_home is a custom number of post count to show on homepage.

9 thoughts on “Changing post count on home page in WordPress template

  1. Doug C.

    Ah, my apologies – nevermind. I see the “loop” now. Of course you could explain this for those who don’t know a lot about code. That would help.

    Reply
  2. Doug C.

    Well, that whole string of code appears visibly at the top of my home page now. It worked for about two minutes then everything went back to the way it was.

    Any suggestions?

    Reply
    1. admin Post author

      Of course you have to make sure you open < ?php and close ?> php tags if you insert this code into HTML part (should be before < ?php if (have_posts())...) ... ?>

      Reply
  3. Hakan

    Are you is this working? I tried with it but it is not working. I set $posts_home = 12 and my most post count is 25. When I go to second page, I missed 13 posts and it starts from 25th post to show…

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *