Pravayest Pravayest Pravayest Pravayest - 27 days ago 10
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);?>

<ul>
<?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_post_field('post_content', $recent["ID"]).'</p>
</div> ';
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
} ?>
</li>
<?php
}?>
</ul>


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
$content = get_post_field('post_content', $recent["ID"]);
$content = strip_tags($content);
echo substr($content, 0, 10);

just use substr for first 10 words