• This code, residing in the head section of the “single” template page, creates an excerpt for the meta description (which is often the text that appears in search results).

    <?php
    $post = $wp_query->post;
    $descrip = strip_tags($post->post_content);
    $descrip_more = '';
    if (strlen($descrip) > 155) {
    	$descrip = substr($descrip,0,155);
    	$descrip_more = ' ...';
    }
    $descrip = str_replace('"', '', $descrip);
    $descrip = str_replace("'", '', $descrip);
    $descripwords = preg_split('/[\n\r\t ]+/', $descrip, -1, PREG_SPLIT_NO_EMPTY);
    array_pop($descripwords);
    $descrip = implode(' ', $descripwords) . $descrip_more;
    echo '<meta name="description" content="'.$descrip.'">';
    ?>
Viewing 13 replies - 1 through 13 (of 13 total)
  • Thanks! I was looking for this.

    Just wondering, is it possible to make it such that if it’s at the homepage, it draws the description from WP blog Tagline?

    Would appreciate your assistance.

    Thanks!

    <php if (is_home() || is_front_page() ) : $descrip = get_bloginfo('description');
    else :
    [...]
    endif;
    ?>

    ^ I’m sorry but how does that fits into the above?

    By the way, I have this in the header.php which is out of the loop. Does it still works?

    Thanks!

    Thread Starter ericr23

    (@ericr23)

    Using the header.php file, you would use esmi’s loop (probably with elseif as well) to set the meta description for different page types (e.g., single vs. home or index).

    get_bloginfo() does not have to be in the loop.

    <php if (is_home() || is_front_page() ) : $descrip = get_bloginfo('description');
    else :
    $post = $wp_query->post;
    $descrip = strip_tags($post->post_content);
    $descrip_more = '';
    if (strlen($descrip) > 155) {
    	$descrip = substr($descrip,0,155);
    	$descrip_more = ' ...';
    }
    $descrip = str_replace('"', '', $descrip);
    $descrip = str_replace("'", '', $descrip);
    $descripwords = preg_split('/[\n\r\t ]+/', $descrip, -1, PREG_SPLIT_NO_EMPTY);
    array_pop($descripwords);
    $descrip = implode(' ', $descripwords) . $descrip_more;
    echo '<meta name="description" content="'.$descrip.'" />';
    endif;
    ?>

    Is the above correct? Sorry, I’m totally lost when it comes to PHP.

    I only need to differentiate the home page from the rest.

    Thread Starter ericr23

    (@ericr23)

    Looks good! You just need to echo get_bloginfo() along with the html tag:

    if (is_home() || is_front_page() ) :
    echo '<meta name="description" content="'.get_bloginfo('description').'" />';

    Got it to work!

    Thanks for the help.

    This excerpt function seemed to work for me when I initially used it in my header but then I found that the last word from the excerpt kept vanishing. To fix it I removed the line “array_pop($descripwords);” which pulls off the last element of the $descripwords array.

    After removing that line the function works as i originally expected. I’m just curious why that line is part of the function in the first place. What is its purpose? Just looking to understand it more thoroughly.

    Thanks.

    Thread Starter ericr23

    (@ericr23)

    Array_pop is used in case the last “word” is in fact a fragment, since the content is cut off at 156 characters without regard to actual words.

    Thanks ericr23. Now i understand the goal of the code snippet.

    But wouldn’t it be better to only pop that last word after you’ve tested to see whether the description exceeds 155. By replacing the “mandatory” array_pop with the conditional version below the last word will only be dropped if the original excerpt is known to have exceeded 155 characters; otherwise it $descrip will keep the full excerpt intact.

    if ($descrip_more == ‘ …’) {
    array_pop($descripwords);
    }

    Thanks for making me understand this better. I hope my addition is an improvement. It seems to work as it should.

    Thread Starter ericr23

    (@ericr23)

    That’s definitely a worthwhile addition. I knew it would never (famous last words!) be the case with our blog that the content would be that short.

    Eric, i now realize why i ran into this issue which seems so unlikely to occur since post content is unlikely to be under 155 characters. I had already made another slight change to your original function that allows for the meta description to be pulled from the manual excerpt that can be added to posts rather than just from the post content.

    I replaced “$descrip = strip_tags($post->post_content);”
    with
    “if ($post->post_excerpt == “” ) : $descrip = strip_tags($post->post_content);
    else :
    $descrip = strip_tags($post->post_excerpt);
    endif;”

    In this case when you add an appropriate length manual excerpt to the post it would then appear in the meta description without the last word.

    Sorry for the confusion.

    Thread Starter ericr23

    (@ericr23)

    Aha! That was indeed the only case where I thought this might be seen. And again, our blog doesn’t use (or very rarely) separate excerpts, and your adaptation makes good sense.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Create excerpt for meta description’ is closed to new replies.