I'm trying to make a page that has all the posts in it and shows 30% or less of the content of the post. So I'm editing a page template and I can't figure out how to show only 30% of the content. I'm new to this so please don't spam me with "go google it you noob" , stuff like that. Thank you in advance and have a nice day!
2 Answers
Go with the_excerpt.
It does what it is supposed to do. Give you an excerpt of your post.
Traditionally, it is set to display the first 55 words of your post. You can tweak it using filters to give more (or less) if needed to be.
As Marco said, use the excerpt to achieve that.
You can change the the_excerpt() function length using the following filter
function my_excerpt_length($length)
{
return 30; // Here is the new length of your excerpt
}
add_filter('excerpt_length', 'my_excerpt_length');
You can add it to your theme functions.php file.
Alternatively, you may want to create a new function where you can have a better control over the content that will be display, I use the following function to achieve that sometimes:
function excerpt($limit)
{
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit)
{
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).' ';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
You can then use it like this:
<?php echo excerpt(40); // 40 is the number of words that are going to be displayed ?>