简体   繁体   English

有没有办法使用他们的变量(即 %%title%%)在页面内获得 yoast 标题

[英]Is there any way to get yoast title inside page using their variable ( i.e. %%title%%)

I need to create a function so that I can use that inside any page which is outside the wordpress regular page.我需要创建一个函数,以便我可以在 wordpress 常规页面之外的任何页面中使用它。 I mean wp_head() will not be placed there.我的意思是 wp_head() 不会放在那里。 I need it for a purpose.我需要它是有目的的。

The purpose is for amp(ampproject.org) page where I can't use any css or js.目的是用于 amp(ampproject.org) 页面,我不能在其中使用任何 css 或 js。 That's why I need this.这就是为什么我需要这个。 I need to place a function at wp title so that the yoast title be placed there.我需要在 wp 标题处放置一个函数,以便将 yoast 标题放置在那里。

I need something like this:我需要这样的东西:

function yoastVariableToTitle($variable){
    return yoast_vaialble_to_show_title($variable);
}

By Default Yoast takes a format as %%title%% %%page%% %%sep%% %%sitename%% , and stores in wp_postmeta table under _yoast_wpseo_title key.默认情况下Yoast需要的格式%%title%% %%page%% %%sep%% %%sitename%% ,而在商店wp_postmeta下表_yoast_wpseo_title关键。

To only get Title of the page/post:只获取页面/帖子的标题:

function yoastVariableToTitle($post_id) {
    $yoast_title = get_post_meta($post_id, '_yoast_wpseo_title', true);
    $title = strstr($yoast_title, '%%', true);
    if (empty($title)) {
        $title = get_the_title($post_id);
    }
    return $title;
}

There can be 2 possibility with SEO title SEO 标题可能有 2 种可能性

Case I : Admin enters %%title%% %%page%% %%sep%% %%sitename%% in SEO title field then the above code will return Post/Page default title .案例 I :管理员在SEO 标题字段中输入%%title%% %%page%% %%sep%% %%sitename%%然后上面的代码将返回 Post/Page default title

Case II : Admin enters My Custom Title %%page%% %%sep%% %%sitename%% in SEO title field then the above code will return My Custom Title .案例二:管理员在SEO 标题字段中输入My Custom Title %%page%% %%sep%% %%sitename%%然后上面的代码将返回我的自定义标题


To get the full Meta Title of the page/post:要获取页面/帖子的完整元标题:

function yoastVariableToTitle($post_id) {

    $yoast_title = get_post_meta($post_id, '_yoast_wpseo_title', true);
    $title = strstr($yoast_title, '%%', true);
    if (empty($title)) {
        $title = get_the_title($post_id);
    }
    $wpseo_titles = get_option('wpseo_titles');

    $sep_options = WPSEO_Option_Titles::get_instance()->get_separator_options();
    if (isset($wpseo_titles['separator']) && isset($sep_options[$wpseo_titles['separator']])) {
        $sep = $sep_options[$wpseo_titles['separator']];
    } else {
        $sep = '-'; //setting default separator if Admin didn't set it from backed
    }

    $site_title = get_bloginfo('name');

    $meta_title = $title . ' ' . $sep . ' ' . $site_title;

    return $meta_title;
}

Hope this helps!希望这可以帮助!

You have very difficult decisions.你有非常困难的决定。 I have a simpler solution:我有一个更简单的解决方案:

function get_post_title( WP_Post $post ): string {
    $yoast_title = get_post_meta( $post->ID, '_yoast_wpseo_title', true );
    if ( empty( $yoast_title ) ) {
        $wpseo_titles = get_option( 'wpseo_titles', [] );
        $yoast_title  = isset( $wpseo_titles[ 'title-' . $post->post_type ] ) ? $wpseo_titles[ 'title-' . $post->post_type ] : get_the_title();
    }

    return wpseo_replace_vars( $yoast_title, $post );
}

And for description:并进行说明:

function get_post_description( WP_Post $post ): string {
    $yoast_post_description = get_post_meta( $post->ID, '_yoast_wpseo_metadesc', true );
    if ( empty( $yoast_post_description ) ) {
        $wpseo_titles           = get_option( 'wpseo_titles', [] );
        $yoast_post_description = isset( $wpseo_titles[ 'metadesc-' . $post->post_type ] ) ? $wpseo_titles[ 'metadesc-' . $post->post_type ] : '';
    }

    return wpseo_replace_vars( $yoast_post_description, $post );
}

The workaround I use in my plugin using functions from class-frontend.php (yoast's class).我在插件中使用的解决方法是使用 class-frontend.php(yoast 的类)中的函数。 It works outside of the loop, just give it a post's id:它在循环之外工作,只需给它一个帖子的 id:

function convert_yoast_title ($post_id) {
    $string =  WPSEO_Meta::get_value( 'title', $post_id );
    if ($string !== '') {
        $replacer = new WPSEO_Replace_Vars();

        return $replacer->replace( $string, get_post($post_id) );
    } 
    return ''; // if not found - returns empty string
}

只需您可以:

$title = wp_title( '-', false, '' );

Since Yoast 14.0, this is a lot easier now.从 Yoast 14.0 开始,这变得容易多了。 You can get the title of the current page with this code:您可以使用以下代码获取当前页面的标题:

YoastSEO()->meta->for_current_page()->title;

Source: https://developer.yoast.com/blog/yoast-seo-14-0-using-yoast-seo-surfaces/来源: https : //developer.yoast.com/blog/yoast-seo-14-0-using-yoast-seo-surfaces/

I ended up with Raunak Gupta's script, and I had to modify it in order to properly display the blog description in the main page title.我最终得到了 Raunak Gupta 的脚本,我必须修改它才能在主页标题中正确显示博客描述。 Here's the modified variant:这是修改后的变体:

function yoastVariableToTitle($post_id) {

    $yoast_title = get_post_meta($post_id, '_yoast_wpseo_title', true);
    $title = strstr($yoast_title, '%%', true);
    if ( !is_front_page() ) {
        $title = strstr( $yoast_title, '%%', true );
    } else {
        $title = get_bloginfo( 'description' );
    }
    $wpseo_titles = get_option('wpseo_titles');

    $sep_options = WPSEO_Option_Titles::get_instance()->get_separator_options();
    if (isset($wpseo_titles['separator']) && isset($sep_options[$wpseo_titles['separator']])) {
        $sep = $sep_options[$wpseo_titles['separator']];
    } else {
    $sep = '-'; //setting default separator if Admin didn't set it from backed
    }

    $site_title = get_bloginfo('name');

    $meta_title = $title . ' ' . $sep . ' ' . $site_title;

    return $meta_title;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM