Display meta description on page

I use the Wordpress Yoast Seo plugin to generate automatic meta and description tags for all posts. I need to display the meta description created by Yoast seo on the message page. I found this code on the Internet.

<?php echo get_post_meta($post->ID, '_yoast_wpseo_metadesc', true); ?>

So wherever I put this php code, it displays a meta description of the message.

Now the problem is that most of the posts on my blog do not have a meta description in their custom field in the post editor. I use automatic meta tags using the plugin by going to Seo> Header Settings> Meta Description Template. I went through the plugin editor and found wpseo_metadesc_template in it. So I tried this code.

<?php echo get_post_meta($post->ID, 'wpseo_metadesc_template', true); ?>

But it does not display anything. Someone please help me with this.

+3
source share
1 answer

Ok, I checked wpseo_metadesc_template- the javascript variable. This will not work ...
the best thing to do is to check if the description is complete and only echo if it exists:

<?php
$yoast_meta = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);
if ($yoast_meta) { //check if the variable(with meta value) isn't empty
    echo $yoast_meta;
}
?>
+6
source

All Articles