有些WordPress主题为某些特定的页面制作了专门的页面模板文件,比如倡萌的一个主题的使用了一个特定存档页面模板:
<?php
/*
Template Name: archives
*/
?> |
<?php
/*
Template Name: archives
*/
?>
然后在后台发布这个页面时,通过“页面属性”选择该模板

但是,当倡萌切换到其他主题,然后在换回原来的主题的时候,该页面所选的特定模板就变成了“默认模板”,你不得不重新选择,是不是很麻烦?
我们需要的结果应该是这样的:让每个页面记住它们在不同的主题下所选择的模板,切换到哪个主题,就使用哪个主题的模板设置(不会丢失原来的设置,也不会被替换为“默认模板”)。
要实现我们需要的结果,只需下载安装 Remember My Template 插件即可;或者将下面代码(来自该插件)添加到主题的 functions.php 文件:
/**
* When a page's `_wp_page_template` key is updated - duplicate this value with the key
* `_wp_page_template_{theme-name}`. `{theme-name}` is the theme's folder name.
*
* Hooks onto `added_post_meta`
* Hooks onto `updated_post_meta`
*
* @param int $meta_id
* @param int $post_id
* @param string $meta_key
* @param string $meta_value
*/
function rmt_update_post_template_meta( $meta_id, $post_id, $meta_key, $meta_value ){
if( '_wp_page_template' === $meta_key ){
$theme = wp_get_theme();
$name = $theme->template;
if( $name ){
update_post_meta( $post_id, '_wp_page_template_' . $name, $meta_value );
}
}
}
add_action( "updated_post_meta", "rmt_update_post_template_meta", 10, 4 );
add_action( "added_post_meta", "rmt_update_post_template_meta", 10, 4 );
/**
* When retrieving a page's `_wp_page_template` replace this with the value associated with
* `_wp_page_template_{theme-name}`, if it exists. `{theme-name}` is the theme's folder name.
*
* Hooks onto `get_post_metadata`
*
* @param mixed $value This is `null`, unless we over-ride it with our own value.
* @param int $post_id
* @param string $meta_key
* @param bool $single
* @return Ambigous <mixed, string, multitype:, boolean, unknown, string>
*/
function rmt_get_post_template_meta( $value, $post_id, $meta_key, $single ){
if( '_wp_page_template' === $meta_key ){
$theme = wp_get_theme();
$name = $theme->template;
if( $name ){
$template = get_post_meta( $post_id, '_wp_page_template_' . $name, $single );
if( $template && locate_template( $template ) ){
$value = $template;
}
}
}
return $value;
}
add_filter( 'get_post_metadata', 'rmt_get_post_template_meta', 10, 4 ); |
/**
* When a page's `_wp_page_template` key is updated - duplicate this value with the key
* `_wp_page_template_{theme-name}`. `{theme-name}` is the theme's folder name.
*
* Hooks onto `added_post_meta`
* Hooks onto `updated_post_meta`
*
* @param int $meta_id
* @param int $post_id
* @param string $meta_key
* @param string $meta_value
*/
function rmt_update_post_template_meta( $meta_id, $post_id, $meta_key, $meta_value ){
if( '_wp_page_template' === $meta_key ){
$theme = wp_get_theme();
$name = $theme->template;
if( $name ){
update_post_meta( $post_id, '_wp_page_template_' . $name, $meta_value );
}
}
}
add_action( "updated_post_meta", "rmt_update_post_template_meta", 10, 4 );
add_action( "added_post_meta", "rmt_update_post_template_meta", 10, 4 );
/**
* When retrieving a page's `_wp_page_template` replace this with the value associated with
* `_wp_page_template_{theme-name}`, if it exists. `{theme-name}` is the theme's folder name.
*
* Hooks onto `get_post_metadata`
*
* @param mixed $value This is `null`, unless we over-ride it with our own value.
* @param int $post_id
* @param string $meta_key
* @param bool $single
* @return Ambigous <mixed, string, multitype:, boolean, unknown, string>
*/
function rmt_get_post_template_meta( $value, $post_id, $meta_key, $single ){
if( '_wp_page_template' === $meta_key ){
$theme = wp_get_theme();
$name = $theme->template;
if( $name ){
$template = get_post_meta( $post_id, '_wp_page_template_' . $name, $single );
if( $template && locate_template( $template ) ){
$value = $template;
}
}
}
return $value;
}
add_filter( 'get_post_metadata', 'rmt_get_post_template_meta', 10, 4 );
来源:
https://www.wpdaxue.com/remember-my-template.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32456.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57