【问题标题】:Use Yoast Filter to modify value of wp_head output?使用 Yoast 过滤器修改 wp_head 输出的值?
【发布时间】:2022-06-22 00:58:13
【问题描述】:

我想修改我的 wp_head。我正在使用 Yoast 插件,我想在描述元标记之后添加一个新的自定义元标记。我尝试使用此代码添加关键字标签,但在描述标签之后未显示,它显示在较低的位置 这段代码

    /*Display custom meta keywords or the post excerpt */
function add_custom_meta_key(){

#Single Page Meta Description
if( is_single() ){
    $key = get_post_meta( get_the_id(), 'keywords', true);
    if( ! empty( $key )  ){
        $meta_key = esc_html($key);
        echo '<meta name="keywords" content="' . $meta_key . '" />';
    }
}}
add_action( 'wpseo_head', 'add_custom_meta_key', 2 );

【问题讨论】:

    标签: wordpress plugins yoast


    【解决方案1】:

    希望您今天过得愉快。 据我从您的查询和代码中了解到,您正试图一个接一个地添加元描述和元关键字标签。 我认为您应该将钩子从 wpseo_head 更改为 wpseo_metadesc 它将一个接一个地渲染元标记。

    我已经添加了下面的代码示例。

    // Define the add_custom_meta_key callback 
    function add_custom_meta_key($wpseo_replace_vars)
    {
        if (is_single()) {
            $key = get_post_meta(get_the_id(), 'keywords', true);
            if (!empty($key)) {
                $meta_key = esc_html($key);
                echo '<meta name="keywords" content="' . $meta_key . '" />';
            }
        }
        return $wpseo_replace_vars;
    };
    add_filter('wpseo_metadesc', 'add_custom_meta_key', 10, 1);
    

    此代码将提供此图像中显示的输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 2018-07-16
      • 2020-05-15
      • 1970-01-01
      • 2014-07-19
      相关资源
      最近更新 更多