John Carter
John Carter

Reputation: 125

Displaying Meta Description On A Page

I'm using Wordpress Yoast Seo plugin to generate automatic meta and description tags of all posts. I need to display meta description generated by Yoast seo on the post page. I found this code on the Internet.

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

So wherever I place this php code, it displays the meta description of the post.

Now the problem is most of the posts in my blog don't have meta description in their custom field on the post editor. I use auto meta tags using the plugin by going to Seo > Title Settings > Meta Description Template. I went through the editor of the plugin 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 displays nothing. Someone please help me out on this.

Upvotes: 4

Views: 18603

Answers (1)

janw
janw

Reputation: 6662

Okay I checked wpseo_metadesc_template is a javascript variable. It won't work...
the best thing to do is check if a description if filled and only echo it 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;
}
?>

Upvotes: 10

Related Questions