WordPress.org

Ready to get started?Download WordPress

Forums

Create excerpt for meta description (14 posts)

  1. ericr23
    Member
    Posted 3 years ago #

    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.'">';
    ?>
  2. Truth
    Member
    Posted 3 years ago #

    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!

  3. esmi
    Forum Moderator
    Posted 3 years ago #

    <php if (is_home() || is_front_page() ) : $descrip = get_bloginfo('description');
    else :
    [...]
    endif;
    ?>
  4. Truth
    Member
    Posted 3 years ago #

    ^ 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!

  5. ericr23
    Member
    Posted 3 years ago #

    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.

  6. Truth
    Member
    Posted 3 years ago #

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

  7. ericr23
    Member
    Posted 3 years ago #

    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').'" />';
  8. Truth
    Member
    Posted 3 years ago #

    Got it to work!

    Thanks for the help.

  9. drh16
    Member
    Posted 2 years ago #

    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.

  10. ericr23
    Member
    Posted 2 years ago #

    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.

  11. drh16
    Member
    Posted 2 years ago #

    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.

  12. ericr23
    Member
    Posted 2 years ago #

    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.

  13. drh16
    Member
    Posted 2 years ago #

    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.

  14. ericr23
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic