【问题标题】:Custom Shortcodes in Yoast SEOYoast SEO中的自定义简码
【发布时间】:2016-12-27 23:30:39
【问题描述】:

我想在 Yoast SEO 插件中使用自定义简码,但我无法使用它。我想将自定义时间规范放在元标题中。

这是我的简码:

function time_yoast_shortcode() {
    $year = date('Y');
    return $year;
}
add_shortcode('yyyy', 'time_yoast_shortcode')

这就是我希望 Yoast 中的标题的样子:这是我在这一年的示例帖子 [yyyy]

有什么想法可以让我的短代码在 Yoast 中工作吗?

【问题讨论】:

    标签: php wordpress shortcode yoast


    【解决方案1】:

    对于希望为 Yoast SEO 的元标题添加短代码支持的任何人,以下是我的处理方式。此代码添加了对 Meta Title 的完整短代码支持,并添加了自定义短代码 [year] 以输出当前年份。将此代码添加到functions.php:

    // Add shortcode support to Yoast Meta Title using 'wpseo_title' filter
    add_filter('wpseo_title', 'filter_wpseo_title');
    function filter_wpseo_title($title) {
      $title = do_shortcode($title);  
      return $title;
    }
    
    // Add [year] shortcode (outputs current date in YYYY format)
    add_shortcode('year', 'year_shortcode');
    function year_shortcode() {
      $year = date('Y');
      return $year;
    }
    

    【讨论】:

      【解决方案2】:

      如果您希望您的帖子在 Yoast 的 SEO 标题末尾显示日期,请尝试:在该特定帖子的 yoast 插件的 SEO 标题字段中的 %%date%%。仅在一年中使用 %%currentyear%%。

      如果您想在全局范围内执行此操作,您可以在插件设置的帖子标题和元数据部分中执行此操作。

      Here is a full list of their varables

      【讨论】:

        【解决方案3】:

        我们创建了一个 WordPress 插件来为 Yoast SEO 执行/运行/显示短代码。它目前在版本 2.1.1 中。不幸的是,由于可能侵犯版权(这是不正确的),我们无法将其上传到 WordPress 存储库。因此,您可以直接从这里下载、使用我们的部分代码并使用安装整个插件: https://denra.com/wordpress/plugins/do-shortcodes-for-yoast-seo/

        我们将在需要时继续使用新功能更新插件,但您可能需要保留我们网站上插件页面的 URL,因为我们无法上传到 WordPress.org

        【讨论】:

          【解决方案4】:

          将此代码添加到 functions.php 文件中,它将起作用:

          // For adding [year] shortcode in WordPress Posts
          
          add_shortcode( 'year', 'sc_year' );
          function sc_year(){
              return date( 'Y' );
          }
          
          add_filter( 'single_post_title', 'my_shortcode_title' );
          add_filter( 'the_title', 'my_shortcode_title' );
          function my_shortcode_title( $title ){
              return do_shortcode( $title );
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-10-05
            • 2023-01-13
            • 2020-07-10
            • 2023-03-05
            • 1970-01-01
            相关资源
            最近更新 更多