0

我有一些自定义设置页面来定义一些全局变量。所以现在我可以打印我的变量:

echo get_option('dealcity');

但是我需要能够在我的页面标题中使用 Yoast 的结果,但是使用 Yoasts 自定义字段代码 %%cf_dealcity%% 不起作用。我猜是因为 dealcity 是一个选项设置而不是自定义字段。所以我想我需要将选项定义为自定义字段。我尝试使用以下内容,然后尝试 %%cf_dealercity%% 但这没有用:

function save_your_fields_meta( $post_id ) {   
$dealercity = get_option('dealcity');
}
add_action( 'save_post', 'save_your_fields_meta' );
4

1 回答 1

0

根据您的代码片段,您可能只是在寻找更新自定义字段save_post?在您的示例中,什么都不会发生,因为您在定义它之后不做任何事情$dealcity,并且您需要将其保存为update_post_meta()

function chrislovessushi_fields_meta( $post_id ){   
    if( $dealcity = get_option( 'dealcity' ) ){
        // Make sure $dealcity exists, then update the post meta
        update_post_meta( $post_id, 'dealcity', $dealcity );
    }
}
add_action( 'save_post', 'chrislovessushi_fields_meta' );

同样对于未来的大脑糖果,您可以使用一些简单的过滤器修改页面标题,例如页面the_title标题和/或标签:wp_title<title>

function chrislovessushi_title_filter( $title, $id = null ) {
    if( is_page() ){
        // Add `dealcity` value before title if this is a page
        $title = get_option('dealcity').' '.$title;
    }
    return $title;
}
add_filter( 'the_title', 'chrislovessushi_title_filter', 10, 2 );
于 2018-03-19T17:11:09.367 回答