In this tutorial, we’ll learn how to add meta descriptions as excerpt in GeneratePress theme. This can be a useful technique for optimizing your website’s search engine rankings and providing users with a more informative preview of your content.
In this article, you’ll learn:
- Use Rank Math or Yoast SEO meta descriptions as excerpts in GeneratePress
- Customizing the “Read More” link at the end of the excerpt
Steps to Add Meta Description as Excerpt in GeneratePress
Here’s how you can add meta descriptions as excerpt in GeneratePress Theme, if you are using either the Rank Math or Yoast SEO plugin.
Here we are using the Code Snippets plugin to insert code in functions.php rather than creating a child theme in GeneratePress Theme.
Step 1: Install the Code Snippets plugin
- Log in to your WordPress dashboard
- Navigate to Plugins > Add New
- Search for “Code Snippets“
- Click “Install Now” and then “Activate”
Step 2: Add a new code snippet
- Go to Snippets > Add New
- Give your snippet a descriptive name (e.g., “Meta Description as an Excerpt in GeneratePress”)
- Paste the following code into the “Code” section
- Click “Activate” to save your changes
Check the code below for Rank Math or Yoast SEO.
Use Rank Math Meta Description as an Excerpt
If you are using Rank Math as SEO Plugin then you can use the following code.
/**
* Use Rankmath meta description as excerpt
*/
function rankmath_description_as_excerpt( $excerpt ) {
if ( ! is_admin() && has_excerpt() ) {
return $excerpt;
}
$description = get_post_meta( get_the_ID(), 'rank_math_description', true );
if ( ! empty( $description ) ) {
$excerpt = wp_strip_all_tags( $description );
$excerpt .= '<p class="read-more-container"><a class="read-more button" href="' . get_permalink() . '">' . apply_filters( 'rankmath_description_as_excerpt_more', __( 'Read More', 'your-text-domain' ) ) . '</a></p>';
}
return $excerpt;
}
add_filter( 'get_the_excerpt', 'rankmath_description_as_excerpt' );
Use Yoast SEO Meta Description as an Excerpt
If you are using Yoast as an SEO Plugin then you can use the following code
/**
* Use Yoast SEO meta description as excerpt
*/
function yoast_description_as_excerpt( $excerpt ) {
if ( ! is_admin() && has_excerpt() ) {
return $excerpt;
}
$description = get_post_meta( get_the_ID(), '_yoast_wpseo_metadesc', true );
if ( ! empty( $description ) ) {
$excerpt = wp_strip_all_tags( $description );
$excerpt .= '<p class="read-more-container"><a class="read-more button" href="' . get_permalink() . '">' . apply_filters( 'yoast_description_as_excerpt_more', __( 'Read More', 'your-text-domain' ) ) . '</a></p>';
}
return $excerpt;
}
add_filter( 'get_the_excerpt', 'yoast_description_as_excerpt' );
Customizing the “Read More” Button
Learn to add and customize the read more button in the GeneratePress Theme. Also, add a read more link if you are using a custom excerpt.
If you want to customize the Read More button then I have already written a tutorial about it.
At the End
By following the steps outlined in this tutorial, you can easily add meta descriptions as excerpt in GeneratePress theme, improving the user experience and potentially increasing your website’s search engine rankings.
If you have any questions or need further assistance, feel free to leave a comment below, and I’ll be happy to help!