• Resolved Christopher Roberts

    (@christopherrobertswordpress)


    Hi everyone
    I am a member of the admin team for a community blog called Technology Bloggers.

    We want to show 8 posts on every page – archives, categories etc.
    We have done this through WordPress by setting the ‘Blog pages show at most * posts‘ to 8 on the Reading Settings page – /wp-admin/options-reading.php

    We however only want to show 3 on the homepage. To do this we are using this code in the Main Index Template (index.php) file

    <?php
    if( is_home() && !is_paged() ) :
    global $query_string;
    query_posts( $query_string . '&posts_per_page=3' );
    endif;
    ?>

    The problem is this means that page 2 starts with post 9 (as it assumes page 1/the front page showed the first 8). The front page only shows 3 posts, so I need page 2 to start on post 4, not 9.

    Make sense?

    Is this possible, if so, please tell me how!

    Thanks
    Christopher – [signature moderated]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter Christopher Roberts

    (@christopherrobertswordpress)

    Thanks, will check it out ??

    Thread Starter Christopher Roberts

    (@christopherrobertswordpress)

    I have tried putting it on the index page, where the old code was, but the homepage just showed a blank page. I have tried putting it in the loop, but am not sure where.

    Where exactly does the code go? Does it replace existing code?

    The code below is right yeah?

    [code moderated - please use the pastebin for any code over the forum limit of 10 lines]

    Thanks again for your help.

    Exactly where it goes depends on your theme. From your original post, it seems it should go in index.php.

    Please put the code there and post the entire file to a pastebin.

    Thread Starter Christopher Roberts

    (@christopherrobertswordpress)

    Here is the current code: https://pastebin.com/aLSsVfxA

    Here is the code with the addition of the new code: https://pastebin.com/iNACWh1J

    In the new version, the following code was replaced with the new code:

    <?php
    if( is_home() && !is_paged() ) :
    global $query_string;
    query_posts( $query_string . '&posts_per_page=3' );
    endif;
    ?>

    Any ideas what’s wrong?

    The TwentyTen theme structure is so different from other themes that it is difficult to use this code in it.

    The basic problem is that loop.php includes the ‘if (have_posts())’ test and the ‘while (have_posts())’ part of the loop. Loop.php is used for all sorts of queries, including galleries, asides, searches, and archives – not just for the home page.

    To keep from messing up those other types of query, you should create a template for the home page (home.php) and use the code from the original index.php (not your modified one), but call get_template_part(‘loop’,’home’).

    Then copy loop.php to loop-home.php and merge the code I gave with that.

    IMHO, you would be better off using a different theme.

    Thread Starter Christopher Roberts

    (@christopherrobertswordpress)

    Okay, I have created a loop-home.php file, which is identical to loop.php does it need to be different in any way?

    I have also created home.php and added in the code from this page https://wordpress.mcdspot.com/2010/04/01/short-first-page/

    Is this code on the home.php correct?

    <?php
    			/* Run the loop to output the posts.
    			 * If you want to overload this in a child theme then include a file
    			 * called loop-index.php and that will be used instead.
    			 */
    			 get_template_part( 'loop', 'index', 'home');
    			?>

    What now? Sorry I am not 100% sure!

    You should create a Child theme to make your changes. If you do not, all of your changes will be lost if you update your theme.

    I think this is what you want:

    • Create home.php by copying index.php.
    • Change get_template_part(‘loop’,’index’) to get_template_part(‘loop’,’home’).
    • Copy the code from this pastebin to loop-home.php.
    Thread Starter Christopher Roberts

    (@christopherrobertswordpress)

    vtxyzzy thank you ever so much, it worked!!!

    We already use a child theme – called twentyten-custom so were the theme to update, our changes would be safe ??

    Thanks for all your help, truly appreciated.
    Christopher

    If your problem has been solved, please use the dropdown on the right to mark this topic ‘Resolved’ so that anyone else with this question can see that there is a solution.

    Thread Starter Christopher Roberts

    (@christopherrobertswordpress)

    Done thanks ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Homepage number of posts’ is closed to new replies.