Products
GG网络技术分享 2025-03-18 16:12 7
I created a separate page for blog posts on my website (WordPress). I Used this code for showing 6 last posts:
<div class=\"container\"><div class=\"entry-content\">
<div class=\"last-posts\">
<?php $the_query = new WP_Query( \'posts_per_page=6\' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class=\"col-md-4\">
<div class=\"post-item\">
<a href=\"<?php the_permalink() ?>\" target=\"_blank\">
<figure><?php the_post_thumbnail(); ?></figure>
</a>
<div class=\"post-detail\">
<h3>
<a href=\"<?php the_permalink() ?>\" target=\"_blank\">
<span><?php the_title(); ?></span>
</a>
</h3>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
</div>
</div>
The problem is, this loop works from \"container\" class to the end of the code, not just 6 last posts. This loads all 6 posts 6 times.
</div>
HI you are making a small mistake in the WP_Query
please try the following line.<?php $the_query = new WP_Query( \'posts_per_page\' => 6 ); ?>
I hope this will work for you.
###
$args = array(\'posts_per_page\' =>6, \'post_type\' => \'post\',\'order\'=>\'desc\',\'post_status\'=>\'publish\');$the_query = new WP_Query( $args );
Demand feedback