Wordpress 短代码未在 Yoast og:title 中呈现

问题描述 投票:0回答:3

我有一个简单的简码,用于帖子标题:

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

add_filter( 'single_post_title', 'my_shortcode_title' );
add_filter( 'the_title', 'my_shortcode_title' );
add_filter('wpseo_title', 'my_shortcode_title');
add_filter( 'wpseo_metadesc', 'my_shortcode_title' );

function my_shortcode_title( $title ){
    return do_shortcode( $title );
}

它工作正常,但我突然意识到 Yoast 创建了一个

og:title
并且它不会在那里渲染(意味着它不会在 Facebook 或 Whatsapp 上渲染)

我寻找答案但找不到任何东西。 有人遇到过这个吗?

也许我需要通过某种过滤器来运行 og:title 这也很好,问题是如何运行。

提前致谢。

wordpress shortcode yoast
3个回答
5
投票

我用过这个,它基本上有效。 它将更新 Yoast 中的 og:title 和社交等。

它不会更新 Yoast 的默认 Schema 输出。这可能需要他们来解决。

// Create Year shortcode to replace with current year.
add_shortcode( 'currentyear', 'current_year' );
function current_year() {
    $year = date( 'Y' );
    return $year;
}
// Activate shortcode function in Post Title.
add_filter( 'the_title', 'do_shortcode' );
add_filter( 'single_post_title', 'do_shortcode' );
add_filter( 'wpseo_title', 'do_shortcode' );
add_filter( 'wpseo_metadesc', 'do_shortcode' );
add_filter( 'wpseo_opengraph_title', 'do_shortcode' );
add_filter( 'wpseo_opengraph_desc', 'do_shortcode' );
add_filter( 'wpseo_opengraph_site_name', 'do_shortcode' );
add_filter( 'wpseo_twitter_title', 'do_shortcode' );
add_filter( 'wpseo_twitter_description', 'do_shortcode' );
add_filter( 'the_excerpt', 'do_shortcode' );

0
投票

您还可以使用“wpseo_schema_webpage”过滤器来替换专门针对网页的架构输出中“名称”字段中的 [year] 短代码。与影响整个 Schema 输出的 wpseo_json_ld_output 过滤器相比,此过滤器更具针对性。

function replace_year_in_yoast_schema( $data ) {
$current_year = date('Y');

if ( isset( $data['@graph'] ) ) {
    foreach ( $data['@graph'] as $key => $value ) {
        if ( isset( $value['name'] ) && strpos( $value['name'], '[year]' ) !== false ) {
            $data['@graph'][$key]['name'] = str_replace('[year]', $current_year, $value['name']);
        }
    }
}

return $data;
}
add_filter( 'wpseo_json_ld_output', 'replace_year_in_yoast_schema', 10, 1 );

0
投票

我这样做了,让您设置需要循环遍历的字段。

function shortcodes_in_yoast_schema( $data ) {
    $siteName = do_shortcode('flexline_site_name');
    $formattedAddress = do_shortcode('flexline_city_state');
    if (isset($data['@graph'])) {
        foreach ($data['@graph'] as $key => $graph) {
            // Define fields to search for placeholders
            $fields = ['description', 'name', 'headline', 'alternateName', 'text']; // Add more fields as needed

            foreach ($fields as $field) {
                if (isset($graph[$field])) {
                    $data['@graph'][$key][$field] = str_replace('[flexline_site_name]', $siteName, $graph[$field]);
                    $data['@graph'][$key][$field] = str_replace('[flexline_city_state]', $formattedAddress, $graph[$field]);
                }
            }
        }
    }
    
    return $data;
}
add_filter( 'wpseo_json_ld_output', __NAMESPACE__ . '\shortcodes_in_yoast_schema', 10, 1 );

© www.soinside.com 2019 - 2024. All rights reserved.