Archived topic
Using Rankmath Meta Description as Excerpt
3 replies · Started by Trevor on October 14, 2022
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
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 :)
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;
} );