Archived topic
Using excerpt as meta description
20 replies · Started by Chad on August 7, 2019
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!
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.
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.
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.
Got it. Thanks a ton!
Glad to be of help
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?
If you want to use a function, you can do this:
add_action( 'wp_head', function() {
echo '<meta name="description" content="' . get_the_excerpt() . '" />';
} );
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.
Awesome, thank you for posting the complete code here! Really appreciate it :)
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?
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.
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() . '" />';
}
}
} );
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 );
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.