- This topic has 20 replies, 4 voices, and was last updated 3 years, 11 months ago by Tom.
-
AuthorPosts
-
August 7, 2019 at 12:26 am #977769Chad
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!
August 7, 2019 at 5:22 am #977934DavidStaffCustomer SupportHi 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.
August 7, 2019 at 5:41 am #977959ChadThat 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.
August 7, 2019 at 5:59 am #977975DavidStaffCustomer SupportYou 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.
August 7, 2019 at 6:06 am #977980ChadGot it. Thanks a ton!
August 7, 2019 at 6:51 am #978005DavidStaffCustomer SupportGlad to be of help
April 26, 2020 at 2:04 am #1256047AnonymousHi 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?
April 26, 2020 at 10:05 am #1256668TomLead DeveloperLead DeveloperIf you want to use a function, you can do this:
add_action( 'wp_head', function() { echo '<meta name="description" content="' . get_the_excerpt() . '" />'; } );
April 26, 2020 at 2:32 pm #1256925AnonymousThanks 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.
April 26, 2020 at 4:30 pm #1257003TomLead DeveloperLead DeveloperAwesome, thank you for posting the complete code here! Really appreciate it 🙂
April 27, 2020 at 12:12 am #1257227AnonymousHi 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?
April 27, 2020 at 9:13 am #1258188TomLead DeveloperLead DeveloperYou 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.
April 27, 2020 at 11:34 pm #1258956AnonymousThanks 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() . '" />'; } } } );
April 27, 2020 at 11:40 pm #1258961AnonymousDigging 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 );
April 28, 2020 at 9:48 am #1259878TomLead DeveloperLead DeveloperThat 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. -
AuthorPosts
- You must be logged in to reply to this topic.