外贸独立站的底层设计密码WordPress 成品站SaaS建站
当前位置:首页>WordPress建站>网站维护>解决 WordPress 4.9 页面模板功能无法正常使用

解决 WordPress 4.9 页面模板功能无法正常使用

[box type=”warning” align=”” class=”” width=””]WordPress 4.9.1 及以上版本已经默认修复该问题,建议直接更新到最新版本,无需再看本文下面的内容了![/box]

WordPress 4.9 有一个重要更新是:在WP后台编辑主题和插件文件时,支持按层级显示所有文件。为了性能考虑,使用了 transient 缓存机制。但是却没有提供任何手动清除缓存的功能,导致有些用户升级到 WordPress 4.9 以后,发现“页面属性 – 模板”这个功能不能正常使用了,不显示主题自带的模板文件!

要解决这个问题,我们可以添加下面的代码到当前主题的 functions.php 就可以了:

/**
 * Plugin name: WP Trac #42573: Fix for theme template file caching.
 * Description: Flush the theme file cache each time the admin screens are loaded which uses the file list.
 * Plugin URI: https://core.trac.wordpress.org/ticket/42573
 * Author: Weston Ruter, XWP.
 * Author URI: https://weston.ruter.net
 */
function wp_42573_fix_template_caching( WP_Screen $current_screen ) {
	// Only flush the file cache with each request to post list table, edit post screen, or theme editor.
	if ( ! in_array( $current_screen->base, array( 'post', 'edit', 'theme-editor' ), true ) ) {
		return;
	}
	$theme = wp_get_theme();
	if ( ! $theme ) {
		return;
	}
	$cache_hash = md5( $theme->get_theme_root() . '/' . $theme->get_stylesheet() );
	$label = sanitize_key( 'files_' . $cache_hash . '-' . $theme->get( 'Version' ) );
	$transient_key = substr( $label, 0, 29 ) . md5( $label );
	delete_transient( $transient_key );
}
add_action( 'current_screen', 'wp_42573_fix_template_caching' );

如果你不想编辑代码,点击下载此插件安装启用即可:wp-42573

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
网站维护

删除 WordPress 中不再使用的自定义文章类型的文章数据

2017-8-20 10:55:52

网站维护

使用Local搭建WordPress本地运行环境:简介及安装

2018-12-27 7:52:14

4 条回复 A文章作者 M管理员
  1. 龙哥

    我试了一下,安装插件也没有用呢

  2. 龙哥

    我的意思是说,按照你说的方法将代码放到主题的functions.php,添加页面的模板还是没有出来,我该怎么办?

  3. 龙哥

    哥子,没有效果啊

  4. 米扑

    代码插件都有,博主的文章,都是精品,让我的博客收益良多

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索