I'm trying to show 2 latest blog posts on my homepage.
However, I want them to appear in 2 separate boxes. This code only shows the same blog post twice. How do I get the second most recent blog post to show in my second box?
Any help would be much appreciated :)
<div class="row boxesl"> <div class="c6"> <?php $the_query = new WP_Query( 'posts_per_page=1' ); ?> <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?> <a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a> <p><?php the_excerpt(__('(more…)')); ?></p> <?php endwhile; wp_reset_postdata(); ?> </div> <div class="c6 last"> <?php $the_query = new WP_Query( 'posts_per_page=1' ); ?> <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?> <a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a> <p><?php the_excerpt(__('(more…)')); ?></p> <?php endwhile; wp_reset_postdata(); ?> </div>
You can use offset
parameter to arguments.
<div class="row boxesl">
<div class="c6">
<?php $the_query = new WP_Query( 'posts_per_page=1' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
<p><?php the_excerpt(__('(more…)')); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<div class="c6 last">
<?php $the_query = new WP_Query( 'posts_per_page=1&offset=1' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
<p><?php the_excerpt(__('(more…)')); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
</div>
Try Below Code :
<div class="row boxesl">
<?php
$the_query = new WP_Query( array(
'post_status' => 'publish',
'orderby' => 'publish_date',
'order' => 'DESC',
'posts_per_page' => 2)
);
if($the_query->have_posts())
{
$cnt=1;
while ($the_query -> have_posts()) : $the_query -> the_post();
if($cnt==2)
$class=" last";
else
$class="";
?>
<div class="c6 <?php echo $class;?>">
<a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
<p><?php the_excerpt(__('(more…)')); ?></p>
</div>
<?php
$cnt++;
endwhile;
wp_reset_postdata();
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.