Pravayest Pravayest Pravayest Pravayest - 1 year ago 211
PHP Question

Wordpress - get short description of recent post

I have wordpress website and I want to get recent posts. So I use loop

<?php $args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts($args);?>

<?php foreach( $recent_posts as $recent ){?>
<?php echo '<div class="textoverlay">
<a href="' . get_permalink($recent["ID"]) . '"><h1>' . $recent["post_title"].'</h1></a>
<p>'.get_post_field('post_content', $recent["ID"]).'</p>
</div> ';
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
} ?>

I use get_post_field for post description, but I see full description. So is it possible to get short description of post by ID ? For example first 10 words ?

Answer Source
$content = get_post_field('post_content', $recent["ID"]);
$content = strip_tags($content);
echo substr($content, 0, 10);

just use substr for first 10 words

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download