Site logo

Archived topic

Using Rankmath Meta Description as Excerpt

3 replies · Started by Trevor on October 14, 2022

Viewing posts 1–4 of 4

Hi,

I'm following up on a post made previously by another user.

Reference: https://generatepress.com/forums/topic/search-results-excerpt/

I think I understand every in this thread except where to add the suggested code. If I were to add something that looks like this as suggested:

=========
add_filter( 'the_excerpt', function( $excerpt ) {
$my_custom_excerpt = get_post_meta( ‘description’, $post_id );
if ( is_search() && !empty( $my_custom_excerpt )) {
$excerpt = $my_custom_excerpt;
}
return $excerpt;
} );
=========
Where would I drop that snippet? By itself into a specific file? Replacing something else?

Not a developer by trade (obviously) but usually able to trial-end-error my way through most minor edits.

Any guidance would be appreciated.

TW

Thanks for the quick response. Sadly, when trying to add it using Code Snippets I get a gnarly fatal error when trying to activate it. Maybe this one wasn't meant to be...

Hi there,

try this snippet:


add_filter( 'the_excerpt', function( $excerpt ) {

    if ( class_exists( 'RankMath' ) ) {
        global $post;
        $desc = RankMath\Post::get_meta( 'description', $post->ID );
        if ( is_search() && !empty( $desc )) {
            $excerpt = $desc;
        }
    }

    return $excerpt; 

} );
This archived topic is closed to new replies.