【问题标题】:Is there any way to get yoast title inside page using their variable ( i.e. %%title%%)有什么方法可以使用它们的变量(即 %%title%%)在页面内获取 yoast 标题
【发布时间】:2016-12-28 11:49:32
【问题描述】:

我需要创建一个函数,以便我可以在 wordpress 常规页面之外的任何页面中使用它。我的意思是 wp_head() 不会放在那里。我需要它是有目的的。

目的是用于我不能使用任何 css 或 js 的 amp(ampproject.org) 页面。这就是为什么我需要这个。我需要在 wp 标题处放置一个函数,以便将 yoast 标题放置在那里。

我需要这样的东西:

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

【问题讨论】:

  • 你只想要设置我的 Yoast 的标题(名称)吗?或完整的短语 %%title%% %%page%% %%sep%% %%sitename%% ??
  • 完整的短语很好。
  • 我已经更新了我的答案,请检查一下,您需要第二部分(即获取页面/帖子的完整元标题:)。跨度>

标签: php html wordpress amp-html yoast


【解决方案1】:

默认情况下,Yoast 采用%%title%% %%page%% %%sep%% %%sitename%% 格式,并存储在wp_postmeta 表下 _yoast_wpseo_title 键。

仅获取页面/帖子的标题:

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;
}

SEO 标题

有两种可能性

案例 I:管理员在 SEO 标题 字段中输入 %%title%% %%page%% %%sep%% %%sitename%%,然后上面的代码将返回 Post/Page 默认标题。 p>

案例二:管理员在SEO title字段中输入My Custom Title %%page%% %%sep%% %%sitename%%,然后上面的代码将返回My Custom Title


获取页面/帖子的完整元标题:

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;
}

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    你有非常艰难的决定。我有一个更简单的解决方案:

    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 );
    }
    

    关于描述:

    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 );
    }
    

    【讨论】:

      【解决方案3】:

      自从 Yoast 14.0 以来,这变得容易多了。您可以使用以下代码获取当前页面的标题:

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

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

      【讨论】:

        【解决方案4】:

        我在插件中使用的解决方法是使用 class-frontend.php(yoast 的类)中的函数。它在循环之外工作,只需给它一个帖子的 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
        }
        

        【讨论】:

        • 看起来有点像 Yoasts 开发人员的笨拙设计,但它有效
        【解决方案5】:

        你可以这样做:

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

        【讨论】:

          【解决方案6】:

          我最终得到了 Raunak Gupta 的脚本,为了在主页标题中正确显示博客描述,我不得不对其进行修改。这是修改后的变体:

          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;
          
          }
          

          【讨论】:

            猜你喜欢
            • 2011-12-02
            • 1970-01-01
            • 2022-08-19
            • 2015-10-07
            • 2023-03-12
            • 1970-01-01
            • 1970-01-01
            • 2012-10-13
            • 1970-01-01
            相关资源
            最近更新 更多