我已经创建了一个自定义模板。现在,我如何应用模板而不是single.php
默认情况下的帖子?
我想这样做,以便主题升级不会被自定义模板取代。
我已经创建了一个自定义模板。现在,我如何应用模板而不是single.php
默认情况下的帖子?
我想这样做,以便主题升级不会被自定义模板取代。
single.php
从子主题自定义模板:single.php
子主题中的模板文件,以替代父主题的single.php
. 这样,父主题的未来升级将不会取代您的自定义设置。single.php
:single-{post-type}.php
要覆盖的模板文件single.php
.由于您将在默认情况下为帖子使用新模板,如果您创建一个新模板文件并将其命名single-post.php
, 然后默认情况下,所有帖子都将加载此模板,而不是single.php
因为它是如何在WordPress核心的模板层次结构中定义的。
single.php
帖子模板:single-post.php
在您的主题中已经可用,或者您怀疑它可能会在将来的升级中添加,在这种情况下,您唯一的选择是添加自定义插件来更改此行为。您可以使用single_template
过滤器挂钩来实现这一点。插件代码示例:
<?php
/*
Plugin Name: Custom Theme Utility Plugin
Plugin URI: https://wordpress.stackexchange.com/a/307541/110572
Description: Overrides single.php template for posts by default
Version: 1.0.0
Author: Fayaz Ahmed
Author URI: https://www.fayazmiraz.com/
*/
define( \'WPSE_307430_PLUGIN_TEMPLATE_DIR\', plugin_dir_path( __FILE__ ) );
function wpse307430_override_single_template( $template_file, $type, $templates ) {
global $post;
if ( $post->post_type == \'post\' ) {
$custom_single_template = WPSE_307430_PLUGIN_TEMPLATE_DIR . \'single.php\';
$template_length = strlen( $template_file );
// To determine if we want to override single.php from plugin
// Without this check, WordPress template hierarchy will be broken for single posts, we don\'t want that
$single_override = $template_length === 0 || substr_compare( $template_file, "single.php", strlen( $template_file ) - 10, 10) === 0;
if( $single_override && file_exists( $custom_single_template ) ) {
$template_file = $custom_single_template;
}
}
return $template_file;
}
add_filter( \'single_template\', \'wpse307430_override_single_template\', 10, 3 );
在目录中创建此示例插件文件(例如:/theme-util/theme-util.php
) 在插件目录下,激活插件并保留您的自定义single.php
此插件目录中的模板文件为/theme-util/single.php
.这样,只要该插件处于活动状态single.php
此插件目录中的模板文件将覆盖single.php
来自父主题和子主题的模板文件。
我已成功安装模板。我知道如何使用模块、文章和菜单创建。我不知道如何使用代码部分。如何更改css上的样式如何修改模板如何修改主页我已多次尝试在安装目录中查找文件夹,但未能找到。如何查找并更改它。