[Resolved] Display Meta Description of the page/post on the Page Header Element

Home Forums Support [Resolved] Display Meta Description of the page/post on the Page Header Element

Home Forums Support Display Meta Description of the page/post on the Page Header Element

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #929239
    Huy

    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

    #929293
    David
    Staff
    Customer Support

    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.

    #929348
    Huy

    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?

    #929989
    David
    Staff
    Customer Support

    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

    #930056
    Huy

    Hi David

    THank you so so much <3 it works

    #930415
    David
    Staff
    Customer Support

    Awesome – glad to be of help.

    #930629
    Huy

    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!

    #930647
    David
    Staff
    Customer Support

    Is the problem only occurring on Archive pages?

    #930901
    Huy

    Yes. It occurs only on the archive page

    #931185
    David
    Staff
    Customer Support

    I edited the function above can you give that a try?

    #931474
    Huy

    Hi David

    After updating the code, the meta des disappeared :<

    http://prntscr.com/o2kxg8

    #931638
    David
    Staff
    Customer Support

    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.

    #931856
    Huy

    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?

    #932499
    Tom
    Lead Developer
    Lead Developer

    Where is the meta description input for the archives? Is there a setting in the global Yoast options?

    #932808
    Huy

    I edit the meta description for the archive by Yoast SEO, like editing a normal post: http://prntscr.com/o35nwe

Viewing 15 posts - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.