Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to automate editors work by automating excerpts.

My solution works but there are few problems with it:

  1. If a post has images/broken html at the beginning it breaks the layout.

  2. Substring cuts words.

Is there a better solution to automate excerpts or improve my existing code?

<?php if(!empty($post->post_excerpt)) {

      the_excerpt();

    } else {

      echo "<p>".substr(get_the_content(), 0, 160)."...</p>";

    }
?>
share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

The excerpt filter by default cuts your post by a word count, which I think is probably preferable to a character-based substr function like you're doing, and it strings out tags and images as well while doing it.

You can set the number of words to excerpt with the filter excerpt_length (it defaults to 55 words, this function from the codex shows how to change it to 20:)

function new_excerpt_length($length) {
    return 20;
}
add_filter('excerpt_length', 'new_excerpt_length', 999);

If you need to use a character-length based cutoff as in your example, you could fix broken tags and such just by applying an appropriate filter to your output, like this:

$content_to_excerpt = strip_tags( strip_shortcodes( get_the_content() ) ); 
echo "<p>". substr( apply_filters('the_excerpt', $content_to_excerpt), 0, 160)."...</p>";

Note that you're stripping tags and applying the filters before truncating the excerpt, so as not to leave an open tag in your excerpt that will screw up the rest of your layout.

There are a number of great themes out there that deals with excerpts in creative ways, I advise you to take a look at how they do it. Here are a few good blog posts from people who have thought through the issue:

Also, for a really random way of dealing with excerpts, look at the Kirby theme - it tries to implement something like Microsoft Word's autosummarize feature by using css to show only headers and lists (from what I remember).

share|improve this answer
add comment

You don't really need to do this. The the_excerpt() tag automatically checks for an excerpt, and if none exists it uses the first 55 words of the post's content (with all tags stripped). This excerpt length can be controlled by hooking into the excerpt_length filter.

If you're trying to include html (images, links, etc) in the automatically-generated excerpt, that's kinda tricky. Obviously, breaking a string at an arbitrary number of words or characters might also break any (x)html.

Wordpress has an internal function that can help with this called force_balance_tags(). It's located in the /wp-includes/formatting.php file. This function fairly reliably adds closing tags to open tags in a string. But it's not a cure-all... it doesn't fix incomplete tags (tags that may be cut off in the middle). So it'd be up to you to figure out how to split the string between tags in the first place.

share|improve this answer
    
+1 for the force_balance_tags() function pointer - thats a good one that I'll have to make use of. –  goldenapples Jan 5 '11 at 18:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.