让WordPress记住不同主题下所选择的的页面模板

有些WordPress主题为某些特定的页面制作了专门的页面模板文件,比如的一个主题的使用了一个特定存档页面模板:

1
2
3
4
5
<?php
/*
Template Name: archives
*/
?>

<?php
/*
Template Name: archives
*/
?>

然后在后台发布这个页面时,通过“页面属性”选择该模板

remember-my-template-wpdaxue_com

但是,当切换到其他主题,然后在换回原来的主题的时候,该页面所选的特定模板就变成了“默认模板”,你不得不重新选择,是不是很麻烦?

我们需要的结果应该是这样的:让每个页面记住它们在不同的主题下所选择的模板,切换到哪个主题,就使用哪个主题的模板设置(不会丢失原来的设置,也不会被替换为“默认模板”)。

要实现我们需要的结果,只需下载安装 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 );

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号