最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loop - First x post with another template then the others

programmeradmin9浏览0评论

I want to output the 5 latest posts. But the template of the first 2 must be different than the other 3 posts. So I've created two queries.

The first one shows the 2 posts:

<?php
$loop = new WP_Query( array(
   'post_type' => 'Post',
   'posts_per_page' => 2
   )
);
?>

 <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

      <?php get_template_part('loop-templates/blog-item-big'); ?>

 <?php endwhile; wp_reset_query(); ?>

The other query must show the other posts (limited with 3 posts):

<?php
$loop = new WP_Query( array(
   'post_type' => 'Post',
   'offset' => 2,
   'post_per_page' => 3
   )
);
?>

 <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

      <?php get_template_part('loop-templates/blog-item-small'); ?>

 <?php endwhile; wp_reset_query(); ?>

The first loop seems to work perfectly. It only shows the 2 last added posts. The second loop seems to start after the second post because of the offset parameter. But it shows more than 3 posts.

What is wrong with that query?

I want to output the 5 latest posts. But the template of the first 2 must be different than the other 3 posts. So I've created two queries.

The first one shows the 2 posts:

<?php
$loop = new WP_Query( array(
   'post_type' => 'Post',
   'posts_per_page' => 2
   )
);
?>

 <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

      <?php get_template_part('loop-templates/blog-item-big'); ?>

 <?php endwhile; wp_reset_query(); ?>

The other query must show the other posts (limited with 3 posts):

<?php
$loop = new WP_Query( array(
   'post_type' => 'Post',
   'offset' => 2,
   'post_per_page' => 3
   )
);
?>

 <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

      <?php get_template_part('loop-templates/blog-item-small'); ?>

 <?php endwhile; wp_reset_query(); ?>

The first loop seems to work perfectly. It only shows the 2 last added posts. The second loop seems to start after the second post because of the offset parameter. But it shows more than 3 posts.

What is wrong with that query?

Share Improve this question asked Jul 13, 2020 at 16:32 DennisDennis 1358 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

It shows more than 3 posts because you used post_per_page when it should be posts_per_page (note the plural "posts").

But actually, you can use the same query and use a counter to display the different template parts:

$loop = new WP_Query( array(
    'post_type'      => 'post',
    'posts_per_page' => 5,
) );

$c = 1; // post counter
while ( $loop->have_posts() ) : $loop->the_post();
    if ( $c < 3 ) : // the first and second posts
        get_template_part( 'loop-templates/blog-item-big' );
    else :
        get_template_part( 'loop-templates/blog-item-small' );
    endif;

    $c++;
endwhile;
/* Or a simpler loop..
$c = 1; // post counter
while ( $loop->have_posts() ) : $loop->the_post();
    $template = 'blog-item-' . ( $c < 3 ? 'big' : 'small' );
    get_template_part( 'loop-templates/' . $template );

    $c++;
endwhile;
*/

// not wp_reset_query()
wp_reset_postdata();
发布评论

评论列表(0)

  1. 暂无评论