Search code examples
phpwordpressposts

Different amount of posts between pages. Options


I have a blog and I need the first post to be the featured post, therefore I need to give that post a special style. That's no problem and I achieved it with this code below.
BUT:
THE problem is that below the first post you find the rest which are lying in two rows of 4 posts each. So basically I have to tell wordpress via the admin interface to give me 9 post per page: the featured post + the two 4 posts rows = 9. All good until you go to second page, where there isn't a featured post, and you get 3 rows, 2x4 and 1 awkward 3rd 1 post row (of course, i asked for 9 p/p).
I've read many posts about this being the answers not clear enough for me. question: Is it possible to get the first post with wp_query and then the normal loop for the rest with an offset of 1 post?. If not, any other simple way?
check it out here

<?php get_header(); ?>
  <div class="container blog">
   <div class="main-column clearfix">
    <?php if (paginate_links()) : ?>
        <div class="paginate">
        <?php echo paginate_links();?>
        </div>
    <?php endif;?>
    <?php
        $primera = true;
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
        if (have_posts()) :
            while(have_posts()) : the_post(); 
            if($primera == true && $paged == 1) { // IF first post AND first page
                get_template_part('portada');
            }
            else { 
                get_template_part('blog');
            }
            $primera = false;
            endwhile;
        else : echo 'No content';
        endif;
    ?>

</div>
<div class="paginate">
            <?php echo paginate_links();?>
</div> 
<?php get_footer(); ?>

Solution

  • I'll suggest you to limit the number of posts query from within the code not from the admin panel. So when you check if its first page then you query 9 posts , but if its other pages you query only 8 posts. Some example codes are ( haven't tested)

    <?php
            $primera = true;
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
            if($paged == 1){
               query_posts( 'posts_per_page=9' );
            }else{
               query_posts( 'posts_per_page=8' );
            }
            if (have_posts()) :
                while(have_posts()) : the_post(); 
                if($primera == true && $paged == 1) { // IF first post AND first page
                    get_template_part('portada');
                }
                else { 
                    get_template_part('blog');
                }
                $primera = false;
                endwhile;
            else : echo 'No content';
            endif;
        ?>
    

    You can looks for more example of query posts before the loop from https://codex.wordpress.org/The_Loop