Bojack Bojack - 4 months ago 21
PHP Question

Get a limited, text-only excerpt from a wordpress post?

I'm using The Loop in my own webpage to get the last three posts from WP.

$args = array( 'numberposts' => 3 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>

<!-- DATE -->
<div class="date">
<?php the_time('m F Y');?>

<!-- TITLE -->
<div class="title">
<?php the_title(); ?>

<!-- SNIPPET -->
<div class="content">
<?php the_excerpt(); ?>
<?php endforeach; ?>

Everything is working fine - except
. I need about 15-20 words of plain text from the post to show as a preview. How do i go about doing this?

Thanks :)


You could try using something like this to grab the first 20 words of the post if there is no excerpt available.

$content = get_the_content();
echo substr($content, 0, 20);