有些WordPress主題為某些特定的頁面制作瞭專門的頁面樣版文件,比如的一個主題的使用瞭一個特定存檔頁面樣版:
1 2 3 4 5 |
<?php /* Template Name: archives */ ?> |
<?php
/*
Template Name: archives
*/
?>
然後在後臺發佈這個頁面時,通過“頁面屬性”選擇該樣版
但是,當切換到其他主題,然後在換回原來的主題的時候,該頁面所選的特定樣版就變成瞭“默認樣版”,你不得不重新選擇,是不是很麻煩?
我們需要的結果應該是這樣的:讓每個頁面記住它們在不同的主題下所選擇的樣版,切換到哪個主題,就使用哪個主題的樣版設置(不會丟失原來的設置,也不會被替換為“默認樣版”)。
要實現我們需要的結果,隻需下載安裝 Remember My Template 外掛即可;或者將下面代碼(來自該外掛)添加到主題的 functions.php 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/** * 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 );