skip to Main Content

I’m looking for a way to show Yoast Seo Title in the frontend as shortcode.

For example: for showing page title as shortcode I’ve added

<?php function page_title_sc( ){ return get_the_title(); } add_shortcode( ‘page_title’, ‘page_title_sc’ );

to the functions.php then added [page_title]to the frontend, and it’s ok. But I want the SEO page title (yoast seo). Any ideas?

2

Answers


  1. Chosen as BEST ANSWER

    Thank you for responding.

    It returns page title, not seo title. :( I'm interested in showing seo title on every page (not only on posts).


  2. This should grab the Yoast SEO title for a post. Put this in your shortcode callback and return $title:

    $title = get_post_meta($post_id, '_yoast_wpseo_title', true);
    
    // so
    
    
    function page_title_sc() {
        global $post_id;
    
        $title = get_post_meta($post_id, '_yoast_wpseo_title', true);
    
        // may want to fallback to default title if SEO title is not set
        if (!$title) {
            $title = get_the_title($post_id);
        }
    
        return $title;
    }
    add_shortcode( 'page_title', 'page_title_sc' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search