How to Add Meta Description as Excerpt in Astra Theme?

In this tutorial, we’ll learn how to add meta description as excerpt in Astra theme. This technique can help optimize your website’s search engine rankings and provide 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 Astra
  • Customize the “Read More” link at the end of the excerpt

Steps to Add Meta Description as Excerpt in Astra

Here’s how you can add meta descriptions as excerpts in the Astra Theme using the Rank Math or Yoast SEO plugin.

How to Add Meta Description as Excerpt in Astra Theme?

We will use the Code Snippets plugin to insert code in functions.php instead of creating a child theme for Astra.

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 name (e.g., “Meta Description as Excerpt in Astra”)
  • Paste the relevant code into the “Code” section (see below)
  • Click “Save Changes and Activate”

Check the code below for Rank Math or Yoast SEO.

Use Rank Math Meta Description as an Excerpt

If using the Rank Math SEO plugin, use this code:

/**
 * Use Rank Math 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 using Yoast SEO plugin, use this code:

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” Link

To customize the appearance of the “Read More” link, you can edit the CSS styles for the .read-more-container and .read-more classes in your theme’s style.css file or the WordPress customizer.

Also Read:

To Sum Up

Following this tutorial, you can easily add a meta description as an excerpt in the Astra theme, potentially improving user experience and search engine rankings.

If you have any other questions, feel free to ask in the comments.

Share:

I am obsessed with WordPress and write articles about WordPress Speed Optimization, SEO, and Blogging. I mostly publish tutorials on GeneratePress, Astra, Kadence, and GenerateBlocks.

Leave a Comment