Wordpress 页面模板不出现在模板下拉列表中

Wordpress page template dont appear inside Template Dropdown List

我刚刚在我的 WP 主题文件夹中添加了一个新的自定义模板文件。 这个新模板像我的其他模板文件一样开始:

<?php
/*
Template Name: My Template Name
*/

奇怪的是,这个新模板没有出现在管理页面编辑的模板下拉列表中。

它似乎有 WP 缓存问题或类似问题...我已尝试清除浏览器的 cookie 和缓存、清除服务器缓存等...但它不起作用。

在网上搜索和测试了一个多小时后,我发现我需要更改主题版本以使 WP 知道新的文件结构(在 style.css ) :

/*
Theme Name: My Theme Name
Version: 1.0.0
*/ 

/*
Theme Name: My Theme Name
Version: 1.0.1
*/ 

终于成功了! 希望它能帮助别人 ;)

如果您安装了 WP-CLI,请尝试 运行 wp cache flush

您可以将此代码放入您的 functions.php

function fix_template_caching( WP_Screen $current_screen ) {
    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', 'fix_template_caching' );

参考:Fix for theme template file caching https://gist.github.com/westonruter/6c2ca0e5a4da233bf4bd88a1871dd950

:)

对我来说,解决方案是更改丢失模板的文件权限。由于某些原因,我上传文件时权限不正确,但将其chmod权限更改为755后,模板如预期出现在下拉列表中。

参考:https://vanseodesign.com/wordpress/wp-page-templates-dropdown/