很抱歉,我没有50%的声誉来添加评论
dot1\'s的答案非常适合在标题部分包含链接替换从post meta获取值,但现在您需要为hreflang添加自定义元键,并从post edit屏幕为每个页面/帖子添加替换值,或者创建一个元框来执行此操作。
// alternate link meta box hook into WordPress
add_action( \'admin_init\', \'add_my_link_metabox\' );
// Add meta box
function add_my_link_metabox() {
// for posts
add_meta_box( \'link_meta_box\', __( \'Alternate Link Attr\', \'Your Text Domain\' ), \'my_link_data_function\', \'post\', \'side\', \'core\' );
// for pages
add_meta_box( \'link_meta_box\', __( \'Alternate Link Attr\', \'Your Text Domain\' ), \'my_link_data_function\', \'page\', \'side\', \'core\' );
}
// create a function to display metabox
function my_link_data_function() {
global $post, $post_id;
$alternate = get_post_meta($post->ID, \'alternate\', true);
$hreflang = get_post_meta($post->ID, \'hreflang\', true);
// generate html for metabox ?>
<div class="admin-inside-box">
<p>
<input type="hidden" name="my_meta_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>" />
</p>
<p>
<label>Link alternate</label>
<input type="text" class="widefat" name="alternate" value="<?php echo $alternate; ?>" />
</p>
<p>
<label>Link hreflang</label>
<input type="text" class="widefat" name="hreflang" value="<?php echo $hreflang; ?>" />
</p>
</div>
<?php
}
// now save metabox data on save post
// hook in save_post action
add_action( \'save_post\', \'save_my_link_data\' );
// create function to save values
function save_my_link_data() {
global $post, $post_id;
// verify nonce
if ( !wp_verify_nonce( $_POST[\'my_meta_nonce\'], basename(__FILE__) ) ) {
return $post_id;
}
// check if current user can edit post
if ( !current_user_can( \'edit_post\', $post_id ) ) {
return $post_id;
}
// now save/update the values in post meta keys
update_post_meta( $post_id, \'alternate\', $_POST[\'alternate\'] );
update_post_meta( $post_id, \'hreflang\', $_POST[\'hreflang\'] );
}
// hook alternate link into wp_head
add_action(\'wp_head\', \'add_link_in_head\');
function add_link_in_head(){
global $post;
if (!empty($post)) {
$alternate = get_post_meta($post->ID, \'alternate\', true);
$hreflang = get_post_meta($post->ID, \'hreflang\', true);
if (!empty($alternate) && !empty($hreflang)) { ?>
<link rel="alternate" href="<?php echo $alternate; ?>" hreflang="<?php echo $hreflang; ?>" /><?php
}
}
}
希望这对您有所帮助,您可以更改函数名等。