WordPress.org

Forums

[resolved] Using the_excerpt for meta description (9 posts)

  1. ajkohn2001
    Member
    Posted 3 years ago #

    I'm looking to create a default meta description by using the_excerpt. This would be the backup when a meta description wasn't in place using the AIO SEO plugin.

    I've read countless articles and tweaked and massaged this but it still won't work. The version below simply outputs "Array"

    <?php if(get_post_meta($post->ID, '_aioseop_description')){ ?>
    <meta itemprop="description" content="<?php echo get_post_meta($post->ID, '_aioseop_description', $single = true); ?>" />
    <?php } else { ?>
    <meta itemprop="description" content="<?php echo get_post_meta($post->ID, the_excerpt(), $single = true); ?>" />
    <?php } ?>

    I am a novice that likes to tinker but this has me stumped. I'd appreciate any help in figuring out why the_excerpt isn't working in this instance and how to fix it.

    Thanks in advance.

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

    your usage is not a suitable way to retrieve the excerpt;
    the_excerpt() http://codex.wordpress.org/Function_Reference/the_excerpt

    also, directly using the_excerpt() will add invalidating html tags to the meta description.

    try:

    <?php if(get_post_meta($post->ID, '_aioseop_description')){ ?>
    <meta itemprop="description" content="<?php echo get_post_meta($post->ID, '_aioseop_description', true); ?>" />
    <?php } else { ?>
    <meta itemprop="description" content="<?php echo esc_attr(htmlentities(get_the_excerpt()));  ?>" />
    <?php } ?>
  3. ajkohn2001
    Member
    Posted 3 years ago #

    I very much appreciate the reply!

    I've read the_excerpt and I have a feeling you can't call it from the head but instead have to use it in conjunction with a function but I've tried a number of methods and they all seem to fail.

    I tried your version and it returns an empty field:

    <meta itemprop="description" content="" />

    I tried this but couldn't get it to work.

  4. alchymyth
    Forum Moderator
    Posted 3 years ago #

    it might work by using setup_postdata();

    example:

    <?php if( is_singular() ) { ?> <meta itemprop="description" content="<?php setup_postdata($post); echo esc_attr(htmlentities(get_the_excerpt()));  ?>" /><?php } ?>

    the only caveat is that in non-singlular pages, the output is unpredictable the excerpt of one of the posts.
    so maybe, the whole needs to get wrapped into a conditional statement as in the above example.

  5. ajkohn2001
    Member
    Posted 3 years ago #

    Hey, that seems to work for the most part. Here's a sample:

    <meta itemprop="description" content="I’m a bit of a minimalist when it comes to my wallet, never wanting to come close to having the dreaded" />

    Just have to get the encoding right. That's not showing up here though. It also seems to be tripping over links and images but I think there's a way to strip those out.

    So, if it's a single post you're ... creating the postdata for that post, then echoing the get_the_excerpt and escaping HTML attributes in the process. Do I have that right?

    I'm thrilled it works but I'd also like to learn exactly why it works too ;)

  6. alchymyth
    Forum Moderator
    Posted 3 years ago #

    http://codex.wordpress.org/Function_Reference/get_the_excerpt
    get_the_excerpt() gets the handwritten excerpt or creates an excerpt from the post content, and returns it as a string;

    http://codex.wordpress.org/Template_Tags/get_posts
    setup_postdata() somehow creates the data for the wordpress template tags from the post object;

    http://php.net/manual/en/function.htmlentities.php
    html_entities() changes quotation marks etc in the excerpt text, so that they don't break the html structure of the meta description tag;

    http://codex.wordpress.org/Function_Reference/esc_attr
    esc_attr() does somthing similar; for instance changes html tags which might be added to the excerpt by filters such as 'like' plugins or a linked 'read more', into harmless characters.

  7. ajkohn2001
    Member
    Posted 3 years ago #

    Thanks so much alchymyth! This makes sense.

    The excerpt seems to stop at the first link in the text instead of grabbing all 55 words. Is there another function for that?

    Again, thank you for your code savvy and quick reply.

  8. alchymyth
    Forum Moderator
    Posted 3 years ago #

    The excerpt seems to stop at the first link in the text instead of grabbing all 55 words. Is there another function for that?

    as far as i know, there is no other standard function;
    does your theme possibly have any code in functions.php to change the excerpt length?
    or are you using a handwritten excerpt in the post?

  9. ajkohn2001
    Member
    Posted 3 years ago #

    No, the theme only has one function related to the sidebar.

    It's okay, this will do for now, particularly since it's only the backup meta description if I haven't crafted one already ... which I always do.

    Thanks again!

Topic Closed

This topic has been closed to new replies.

About this Topic