You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何将WordPress选项设置转为自定义字段以适配Yoast页面标题

Solution for Using Global Option Value in Yoast SEO Title

Hey there, let's get that global dealcity option showing up in your Yoast page titles. The core issue here is that Yoast's %%cf_dealcity%% variable only works with post-specific custom fields (stored in wp_postmeta), while your dealcity is a site-wide option (stored in wp_options). Even if you tried manually adding it as a custom field, that'd only apply to individual posts—not globally.

Here are two solid solutions to fix this:

Option 1: Create a Custom Yoast Variable

This lets you add a dedicated %%dealcity%% variable to Yoast's replacement options, just like the built-in ones. Add this code to your theme's functions.php file (or a custom plugin for better portability):

// Add custom replacement logic for our dealcity option
add_filter('wpseo_replacements', 'add_dealcity_yoast_variable', 10, 2);
function add_dealcity_yoast_variable($replacements, $type) {
    // Target only title replacements (remove this check to use in meta descriptions too)
    if ($type === 'title') {
        $dealcity_value = get_option('dealcity');
        // Sanitize the value for safe use in titles
        $replacements['%%dealcity%%'] = !empty($dealcity_value) ? esc_html($dealcity_value) : '';
    }
    return $replacements;
}

// Register the variable in Yoast's backend so you can select it easily
add_filter('wpseo_available_replacements', 'register_dealcity_yoast_variable');
function register_dealcity_yoast_variable($available_replacements) {
    $available_replacements['dealcity'] = 'Global Deal City Option';
    return $available_replacements;
}

How to use it:

  1. Save the code and refresh your WordPress admin.
  2. Go to any page/post's Yoast SEO settings, edit the title field.
  3. You'll now see %%dealcity%% in the list of available variables—insert it wherever you want the value to appear.

Option 2: Directly Modify the Yoast Title Output

If you prefer a more straightforward approach without adding a new variable, use the wpseo_title filter to inject the dealcity value directly into the final title string:

add_filter('wpseo_title', 'insert_dealcity_into_title');
function insert_dealcity_into_title($original_title) {
    $dealcity_value = get_option('dealcity');
    
    if (!empty($dealcity_value)) {
        // Example 1: Replace a placeholder in your Yoast title (e.g., {dealcity})
        $modified_title = str_replace('{dealcity}', esc_html($dealcity_value), $original_title);
        
        // Example 2: Prepend the dealcity value to the title
        // $modified_title = esc_html($dealcity_value) . ' | ' . $original_title;
        
        return $modified_title;
    }
    
    // If no dealcity value exists, return the original title
    return $original_title;
}

How to use it:

  1. Pick either example (or adjust to your needs) and add the code to functions.php.
  2. In your Yoast title settings, use {dealcity} as a placeholder (if using Example 1) or let the code automatically prepend the value (Example 2).

Important Notes:

  • Always backup your functions.php before editing it, or use a child theme to avoid losing changes when your parent theme updates.
  • Both methods pull the global dealcity value directly via get_option('dealcity'), so any updates to your custom settings page will automatically reflect in the Yoast titles.

内容的提问来源于stack exchange,提问作者chrislovessushi

火山引擎 最新活动