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

若要前行,就得离开你现在停留的地方。要有对自己纯粹而坚韧的标准,才可以跟着直觉和信念走去。自卑心最伤害自己,是最大的障碍。有时候阳光很好,有时候阳光很暗,这就是生活。

有些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 );

到此这篇关于让WordPress记住不同主题下所选择的的页面模板就介绍到这了。平凡人生要用平凡的心去对待,你的人生将会更精彩。更多相关让WordPress记住不同主题下所选择的的页面模板内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?

WordPress安装在主机空间的什么目录里面?根目录在哪里?