[resolved] Automatic Excerpt is Not Working (3 posts)

  1. chadrew
    Posted 5 years ago #

    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.



    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?

  2. chadrew
    Posted 5 years ago #

    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?

  3. chadrew
    Posted 5 years ago #

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


    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)
    		$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.

Topic Closed

This topic has been closed to new replies.

About this Topic