I can’t work out how to change the number of posts listing on the first blog page only. I will need to list 4 normal posts on the first page and 6 normal posts on any other page. I found a few examples on StackExchange but my case is slightly different because I also have Featured post on the first page, which belongs to the Featured category and 4 other normal posts. From the second page on, there is no Featured post showing, only 6 normal posts.
This code works really well with the Featured posts, but can’t find a way to change the number of normal posts showing. Every time I add new WP_Query()
for the second loop the Featured post stops working.
<ul class="grid grid--blog">
<?php $do_not_duplicate = 0;
if(!is_paged()):
$featured = new WP_Query('category_name=featured&posts_per_page=1');
while ($featured->have_posts()) : $featured->the_post();
$do_not_duplicate = $post->ID; ?>
<li class="grid__item grid__item-featured"><?php get_template_part( 'entry' ); ?></li>
<?php endwhile;
endif;
if (have_posts()) :
while (have_posts()) : the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<li class="grid__item"><?php get_template_part( 'entry' ); ?></li>
<?php endwhile;
endif;
?>
</ul>
and this is the desired layout:
First Blog listing page
All the other Blog listing pages
Any help would be very appreciated!
I can’t work out how to change the number of posts listing on the first blog page only. I will need to list 4 normal posts on the first page and 6 normal posts on any other page. I found a few examples on StackExchange but my case is slightly different because I also have Featured post on the first page, which belongs to the Featured category and 4 other normal posts. From the second page on, there is no Featured post showing, only 6 normal posts.
This code works really well with the Featured posts, but can’t find a way to change the number of normal posts showing. Every time I add new WP_Query()
for the second loop the Featured post stops working.
<ul class="grid grid--blog">
<?php $do_not_duplicate = 0;
if(!is_paged()):
$featured = new WP_Query('category_name=featured&posts_per_page=1');
while ($featured->have_posts()) : $featured->the_post();
$do_not_duplicate = $post->ID; ?>
<li class="grid__item grid__item-featured"><?php get_template_part( 'entry' ); ?></li>
<?php endwhile;
endif;
if (have_posts()) :
while (have_posts()) : the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<li class="grid__item"><?php get_template_part( 'entry' ); ?></li>
<?php endwhile;
endif;
?>
</ul>
and this is the desired layout:
First Blog listing page
All the other Blog listing pages
Any help would be very appreciated!
Share Improve this question asked Aug 28, 2019 at 13:50 KaiKai 1013 bronze badges1 Answer
Reset to default 0I took me a while to wrap my head around this, but finally, I made it work. I will share my solution here in case someone else is looking to implement a similar layout:
<ul class="post preview grid grid--blog">
<?php $do_not_duplicate = 0 ?>
<?php if(!is_paged()): ?>
<?php $args = array(
'category_name' => 'featured',
'posts_per_page' => 1
);
$featured = new WP_Query( $args );
while ( $featured->have_posts() ) : $featured->the_post();
$do_not_duplicate = $post->ID; ?>
<li class="grid__item grid__item-featured"><?php get_template_part( 'entry' ); ?></li>
<?php endwhile;
endif;
wp_reset_query(); ?>
<?php
$featuredid = get_cat_ID( 'Featured' );
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
if(!is_paged()) {
$postsargs = array(
'posts_per_page' => 4,
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'cat' => - $featuredid,
'paged' => $paged
);
$allposts = new WP_Query( $postsargs );
} else {
$secondPageArgs = array(
'posts_per_page' => 6,
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'cat' => - $featuredid,
'paged' => $paged
);
$allposts = new WP_Query( $secondPageArgs );
}
if ( $allposts->have_posts() ) :
while ( $allposts->have_posts() ) : $allposts->the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<li class="grid__item"><?php get_template_part( 'entry' ); ?></li>
<?php endwhile;
endif;
wp_reset_query(); ?>
</ul>
So $do_not_duplicate
makes sure that you don't repeat the Featured post from the first query in the second query when listing all the other posts.
And then wrapping our second query $allposts
arguments in the IF statement to check on which page those posts are going to be displayed. If we are on the first blog page, 'posts_per_page' => 4
will be called, otherwise 'posts_per_page' => 6
will be called on all the other pages.
Hope it helps!