[Resolved] Using excerpt as meta description

Home Forums Support Using excerpt as meta description

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #977769
    Chad

    What would be the best way to use the excerpt as the meta description?

    I’m attempting to go without one of those insane, bloated SEO plugins and just need a way to set the meta description — the excerpt would work.

    I’m okay with tinkering in the templates if needed.

    Thanks!

    #977934
    David
    Staff
    Customer Support

    Hi there,

    a very simple way way to use the Excerpt meta box on the single post would be to add this:

    <meta name="description" content="<?php echo the_excerpt(); ?>" />

    to a Hook Element. Select the WP_Head hook and set display rules to Posts.

    #977959
    Chad

    That is a simple way for sure.

    Is there a good template way to add the code?

    Would I do something in header.php or does GeneratePress prefer another way?

    Curious if there is a teeny tiny performance hit with using GP Elements for this.

    #977975
    David
    Staff
    Customer Support

    You could copy the header.php to your child theme and edit that.
    Alternatively you could add a function to your child functions php eg.

    <?php
    add_action('wp_head', 'your_function');
    
    your_function() {
        if (!is_single() {
            // output something for not single posts
        } else {
            // output something for single posts
        }
    }
    ?>

    Elements are stored as a custom post type – the performance hit would be unnoticeable.

    So lots of options. If i am doing a lot of custom work i would probably write a function. But generally don’t need to do a lot of custom work with GP so would opt for Elements.

    #977980
    Chad

    Got it. Thanks a ton!

    #978005
    David
    Staff
    Customer Support
    #1256047
    Anonymous

    Hi David,

    Does the code you have suggested require to enable PHP execution?

    <meta name="description" content="<?php echo the_excerpt(); ?>" />

    Assuming it does, what would the recommendation be to include it in the functions.php file?

    #1256668
    Tom
    Lead Developer
    Lead Developer

    If you want to use a function, you can do this:

    add_action( 'wp_head', function() {
        echo '<meta name="description" content="' . get_the_excerpt() . '" />';
    } );
    #1256925
    Anonymous

    Thanks Tom.

    That doesn’t appear to work and results in an output in the head ending />” when there is no excerpt for a post.

    It also seems to appear on the home page when it is configured to display posts i.e the excerpt of the first post appears as an excerpt.

    I modified the code as follows to display a specific meta description for the home page and populate posts with the associated descriptions. It only displays it if there is an excerpt.

    add_action( 'wp_head', function() {
        if ( has_excerpt() ) {
        
            if ( !is_home() ) {
                echo '<meta name="description" content="' . get_the_excerpt() . '" />';
            }
            elseif ( is_home() ) {
                echo '<meta name="description" content="some content" />';
            }
        }
    } );

    There doesn’t seem to be a way to display excerpts for pages unless I had the following code in the functions.php. I am assuming that it’s supported by GeneratePress.

    add_post_type_support( 'page', 'excerpt' );

    Hopefully this helps anyone else looking for a similar solution.

    #1257003
    Tom
    Lead Developer
    Lead Developer

    Awesome, thank you for posting the complete code here! Really appreciate it 🙂

    #1257227
    Anonymous

    Hi Tom,

    In selecting to display the excerpts as meta descriptions, it now displays the same content for the blog posts as well on the home page for example.

    This isn’t intended. The blog posts were previously showing 20 characters from the post which is the desired look.

    How can the blog posts show 20 characters for example from the post however the meta descriptions populate the content from the excerpt?

    #1258188
    Tom
    Lead Developer
    Lead Developer

    You would likely need to build a custom field specifically for the meta description. If you’re going to do that, you may as well go with an SEO plugin, as that’s exactly what they do.

    #1258956
    Anonymous

    Thanks Tom.

    I updated the previous code so that it is invoked when there isn’t an excerpt. This now displays the meta description on the home page without needing an excerpt in the first post.

    add_action( 'wp_head', function() {
                
        if ( !has_excerpt() ) {
            if ( is_home() ) {
                echo '<meta name="description" content="some content" />';
            }
        }
        
        elseif ( has_excerpt() ) {
        
            if ( is_home() ) {
                echo '<meta name="description" content="some content" />';
            }
            elseif ( !is_home() ) {
                echo '<meta name="description" content="' . get_the_excerpt() . '" />';
            }
        }
    } );
    #1258961
    Anonymous

    Digging into excerpts displaying in place of the content on the home page, I developed the solution below. Hopefully, it’ll help others looking for a similar option.

    Do you foresee any issues with it?

    function replace_excerpt_with_content( $excerpt ){
        
        if ( is_home() ) {
        
            $more = '<p class="read-more-container"><a title="' . get_the_title() . '" class="read-more button" href="' . get_permalink() .  '">Read More<span class="screen-reader-text">' . get_the_title() . '</span></a></p>';
            $content = force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( wpautop(get_the_content()) ), 20, $more ) ) );  
       
            return $content;
        }
    }
    
    add_filter( 'the_excerpt', 'replace_excerpt_with_content', 10, 1 );
    #1259878
    Tom
    Lead Developer
    Lead Developer

    That code doesn’t return anything if you’re not on the blog page, which will likely prevent excerpts from appearing at all.

    You should return $excerpt before the end of the function.

Viewing 15 posts - 1 through 15 (of 21 total)
  • You must be logged in to reply to this topic.