[Support request] Using Rankmath Meta Description as Excerpt

Home Forums Support [Support request] Using Rankmath Meta Description as Excerpt

Home Forums Support Using Rankmath Meta Description as Excerpt

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2373883
    Trevor

    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

    #2373889
    Leo
    Staff
    Customer Support

    Hi Trevor,

    Try one of these methods:
    Adding PHP: https://docs.generatepress.com/article/adding-php/

    The Code Snippets plugin is the easiest method if you aren’t using a child theme already.

    Let me know if this helps 🙂

    #2374664
    Trevor

    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…

    #2375076
    David
    Staff
    Customer Support

    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; 
    
    } );
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.