I use Wordpress and I want to get recent posts with thumbnail and description.
So I use wp_get_recent_posts.
<?php $args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts($args);?>
<ul class="main-slider">
<?php foreach( $recent_posts as $recent ){?>
<li>
<?php echo '<div class="textoverlay">
<a href="' . get_permalink($recent["ID"]) . '"><h1>' . $recent["post_title"].'</h1></a>
<p>'.get_the_excerpt().'</p>
</div> ';
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
} ?>
</li>
<?php
}?>
</ul>
I can get post title and link, but I can't get description:
<p>'.get_the_excerpt().'</p>
You have just missing of post_id in description without post_id it can't display anything.
<?php $args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts($args);?>
<ul class="main-slider">
<?php foreach( $recent_posts as $recent ){?>
<li>
<?php echo '<div class="textoverlay">
<a href="' . get_permalink($recent["ID"]) . '"><h1>' . $recent["post_title"].'</h1></a>
<p>'.get_the_content($recent["ID"]).'</p>
</div> ';
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
} ?>
</li>
<?php
}?>
</ul>
echo wpautop( $recent['post_content'] );
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.