【问题标题】:How to show Yoast SEO title in the front end as a shortcode?如何在前端将 Yoast SEO 标题显示为简码?
【发布时间】:2020-07-10 22:37:12
【问题描述】:

我正在寻找一种在前端将 Yoast Seo 标题显示为简码的方法。

例如:将页面标题显示为我添加的简码

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

到functions.php,然后在前端添加[page_title],就可以了。但我想要 SEO 页面标题(yoast seo)。有什么想法吗?

【问题讨论】:

    标签: php wordpress frontend yoast


    【解决方案1】:

    这应该获取一个帖子的 Yoast SEO 标题。把它放在你的短代码回调中并返回$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' );
    

    【讨论】:

      【解决方案2】:

      感谢您的回复。

      它返回页面标题,而不是 seo 标题。 :( 我有兴趣在每个页面上显示 seo 标题(不仅在帖子上)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-03
        • 2014-03-28
        • 1970-01-01
        • 1970-01-01
        • 2018-12-11
        • 1970-01-01
        相关资源
        最近更新 更多