WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Automatic Excerpt is Not Working

[Resolved] Automatic Excerpt is Not Working

  • Hello,
    I am trying to use the post excerpt for meta description, like this:

    <?php if ( is_single() ) { ?>
    <meta name="description" content="<?php echo get_the_excerpt() ?>" />
    <?php } ?>

    It is working just fine for posts which have an excerpt added. But it is empty for posts which do not have an excerpt.

    Why?

    http://codex.wordpress.org/Function_Reference/the_excerpt

    This page says that “If you do not provide an explicit excerpt to a post (in the post editor’s optional excerpt field), it will display an automatic excerpt which refers to the first 55 words of the post’s content.”

    Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Nevermind, I missed the part where it said this thing is supposed to be in “The Loop”.

    But somehow, if a post has a manual excerpt added, it does work. Is this normal?

    This took me a while but I think I got it. I borrowed the excerpt function from here:

    http://wordpress.org/extend/plugins/hybrid-bugfix/faq/

    And modified it to limit only automatic excerpts, since for some reason the 22 word limit limited my “automatic” excerpts properly, but my “manual” excerpts to like 20 characters, no idea why!

    In functions.php:

    function blabla($excerpt_word_count=24)
    {
        global $post;
        $excerpt = $post->post_excerpt;
        if( $excerpt == '' )
        {
            $excerpt = $post->post_content;
            $excerpt = strip_shortcodes( $excerpt );
            $excerpt = str_replace(']]>', ']]>', $excerpt);
            $excerpt = strip_tags($excerpt);
    	$words = explode(' ', $excerpt, $excerpt_word_count + 1);
    	if (count($words) > $excerpt_length)
    		{
    		array_pop($words);
    		$excerpt = implode(' ', $words);
    		$excerpt .= '...';
    		}
        }
        return $excerpt;
    }
    <?php if ( is_single() ) { ?>
    <meta name="description" content="<?php echo blabla(); ?>" />
    <?php } ?>

    I’m not sure if this way is better than the other method I saw with creating another “Loop” to get the excerpt.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Automatic Excerpt is Not Working’ is closed to new replies.
Skip to toolbar