今天所做的努力
都是在为明天积蓄力量

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

本文最后更新于2019年7月6日,已超过1756天没有更新,如果文章内容失效,请留言反馈给我们,谢谢!
强烈向大家推荐一个好网站,【我要自学网】,教程由在校老师录制,有办公会计、平面设计、室内设计、机械设计、网页编程、影视动画等教程.....让你足不出门,都可以体验学校的专业教育!

[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

赞(0)
未经允许不得转载:如需转载,请标注内容来源流觞 » 解决 WordPress 4.9 页面模板功能无法正常使用
分享到: 更多 (0)
强烈向大家推荐一个好网站,【我要自学网】,教程由在校老师录制,有办公会计、平面设计、室内设计、机械设计、网页编程、影视动画等教程.....让你足不出门,都可以体验学校的专业教育!
强烈向大家推荐一个好网站,【我要自学网】,教程由在校老师录制,有办公会计、平面设计、室内设计、机械设计、网页编程、影视动画等教程.....让你足不出门,都可以体验学校的专业教育!

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

今天所做的努力都是在为明天积蓄力量

联系我们关于小站