您可以通过如下修改上述共享代码来实现这一点。
$prelimartPosts = new WP_Query($args2);
while ($prelimartPosts->have_posts() ) : $prelimartPosts->the_post();
if (($prelimartPosts->current_post +2) >= ($prelimartPosts->post_count)) {
// This is last post and second last post
get_template_part( \'template-parts/content\', \'front-center\' );
} else {
// These are all other posts except last and second last posts
get_template_part(\'template-parts/content\', \'front-bottom\');
}
endwhile;
如果要对每个条件使用单独的条件,请使用下面的代码,而不是上面的代码。
$prelimartPosts = new WP_Query($args2);
while ($prelimartPosts->have_posts() ) : $prelimartPosts->the_post();
if (($prelimartPosts->current_post +1) == ($prelimartPosts->post_count)) {
// This is last post
get_template_part( \'template-parts/content\', \'front-center\' );
} else if (($prelimartPosts->current_post +2) == ($prelimartPosts->post_count)) {
// This is second last post
get_template_part( \'template-parts/content\', \'front-center\' );
} else {
// These are all other posts except last and second last posts
get_template_part(\'template-parts/content\', \'front-bottom\');
}
endwhile;