Archived topic
Display Meta Description of the page/post on the Page Header Element
17 replies · Started by Huy on June 14, 2019
Hi guys
I am using Page Header elements for almost pages on my website and really like this feature
I wonder if have any way to display the meta description of the page/post on the Page Header? I looked for some Template tags but it seemed that tag wasn't there
I hope you can help me, thank you so so much
Hi there,
how are you adding the post meta description? With an SEO plugin? As this may require writing a shortcode to use to output the content in the header element.
Hi David
I use Yoast SEO to add the post meta description. I have to first create the shortcode then I can fill it in Header, right?
So we could create a shortcode with this PHP Snippet:
function db_page_hero_excerpt() {
ob_start();
$meta_desc = get_post_meta(get_queried_object_id(), '_yoast_wpseo_metadesc', true);
if ($meta_desc) {
echo '<div class="page-hero-post-meta">' . $meta_desc . '</div>';
}
return ob_get_clean();
}
add_shortcode( 'page_hero_meta_desc','db_page_hero_excerpt' );
Then add the [page_hero_meta_desc] to your header element
Hi David
THank you so so much <3 it works
Awesome - glad to be of help.
Hi David
I have some problem with the code
In this page: https://www.cyberpet.com/dogs/. The meta description of the page is:
"Find posts about dogs that are as irresistible as their puppy eyes. They are curated to give you valuable information on how to care for your canine companion."
However, The meta description o the page is not what it should be. Instead, it picked the meta description of the first post showed up in the category:
"Are you worried about your husky’s picky eating habits or coat health? A change in diet might help! Check out our list of the best dog foods for huskies."
Can you help me, please!
Is the problem only occurring on Archive pages?
Yes. It occurs only on the archive page
I edited the function above can you give that a try?
Does it still display on the Single Posts? If so i am a little stumped by this, you may need to speak with Yoast to see if they can advise on how to display this.
Actually, I only use this code to display the meta description of the archive page. I don't use this in Single Post
Does that make the problem easier?
Where is the meta description input for the archives? Is there a setting in the global Yoast options?
I edit the meta description for the archive by Yoast SEO, like editing a normal post: http://prntscr.com/o35nwe