如果官方主题和Underscores 是某种指导,人们会注意到,与条件标记相比,多个差异很小的文件更受欢迎(archive.php
, search.php
, category.php
, index.php
等等)
我发现存在多个类似的文件让人迷惑,我计划使用条件标记将其中一些文件组合起来,因为它们之间的主要区别是页面的标题和描述。这是否会被视为不良行为?
如果两种方法都有效,是否会对加载速度产生任何影响?哪一个更快?
如果官方主题和Underscores 是某种指导,人们会注意到,与条件标记相比,多个差异很小的文件更受欢迎(archive.php
, search.php
, category.php
, index.php
等等)
我发现存在多个类似的文件让人迷惑,我计划使用条件标记将其中一些文件组合起来,因为它们之间的主要区别是页面的标题和描述。这是否会被视为不良行为?
如果两种方法都有效,是否会对加载速度产生任何影响?哪一个更快?
在我开始之前,以及在你继续阅读文章的其余部分之前,请确保查看我在WordPress中关于模板的回答
。。。。请注意,与条件标记相比,多个差异较小的文件是首选的
模板已通过模板层次结构记帐。只需包含一个模板,如category.php
不需要任何额外工作。要真正理解这一点,让我们看看template loader (,其中还包括template_include
根据需要筛选到稍后的模板层次结构
44 if ( defined(\'WP_USE_THEMES\') && WP_USE_THEMES ) :
45 $template = false;
46 if ( is_404() && $template = get_404_template() ) :
47 elseif ( is_search() && $template = get_search_template() ) :
48 elseif ( is_front_page() && $template = get_front_page_template() ) :
49 elseif ( is_home() && $template = get_home_template() ) :
50 elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
51 elseif ( is_tax() && $template = get_taxonomy_template() ) :
52 elseif ( is_attachment() && $template = get_attachment_template() ) :
53 remove_filter(\'the_content\', \'prepend_attachment\');
54 elseif ( is_single() && $template = get_single_template() ) :
55 elseif ( is_page() && $template = get_page_template() ) :
56 elseif ( is_singular() && $template = get_singular_template() ) :
57 elseif ( is_category() && $template = get_category_template() ) :
58 elseif ( is_tag() && $template = get_tag_template() ) :
59 elseif ( is_author() && $template = get_author_template() ) :
60 elseif ( is_date() && $template = get_date_template() ) :
61 elseif ( is_archive() && $template = get_archive_template() ) :
62 elseif ( is_comments_popup() && $template = get_comments_popup_template() ) :
63 elseif ( is_paged() && $template = get_paged_template() ) :
64 else :
65 $template = get_index_template();
66 endif;
67 /**
68 * Filter the path of the current template before including it.
69 *
70 * @since 3.0.0
71 *
72 * @param string $template The path of the template to include.
73 */
74 if ( $template = apply_filters( \'template_include\', $template ) )
75 include( $template );
76 return;
77 endif;
让我们看看如何为类别页面加载模板。如您所见,当我们访问类别页面时,应该加载的模板由以下行决定57 elseif ( is_category() && $template = get_category_template() ) :
显然是因为is_category()
是条件树中唯一返回true的条件。查看类别页面时要加载的模板部分取决于get_category_template()
作用function get_category_template() {
$category = get_queried_object();
$templates = array();
if ( ! empty( $category->slug ) ) {
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
}
$templates[] = \'category.php\';
return get_query_template( \'category\', $templates );
}
比方说,我们正在查看Cars
类别,段塞为cars
类别id为10
. get_category_template()
生成要搜索的以下模板category-cars.php
category-10.php
category.php
这就是数组的外观:(按重要性/层次结构的顺序)
array(3) {
[0]=>
string(17) "category-cars.php"
[1]=>
string(15) "category-10.php"
[2]=>
string(12) "category.php"
}
这就是传递给get_query_template()
.function get_query_template( $type, $templates = array() ) {
$type = preg_replace( \'|[^a-z0-9-]+|\', \'\', $type );
if ( empty( $templates ) )
$templates = array("{$type}.php");
$template = locate_template( $templates );
/**
* Filter the path of the queried template by type.
*
* The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
* extension and any non-alphanumeric characters delimiting words -- of the file to load.
* This hook also applies to various types of files loaded as part of the Template Hierarchy.
*
* @since 1.5.0
*
* @param string $template Path to the template. See locate_template().
*/
return apply_filters( "{$type}_template", $template );
}
所有这一切的真正决策者是get_query_template()
其中使用locate_template
检查层次结构中的第一个可用模板,然后加载它。所以,首先get_query_template()
将查看是否category-cars.php
存在,如果不存在,它将查找category-10.php
最后是category.php
如果不存在其他模板。返回并加载找到的第一个。如果找不到这些模板,get_query_template()
返回false,并且没有为加载设置模板如果你回到这一行
elseif ( is_category() && $template = get_category_template() ) :
因为$template = get_category_template()
返回false,条件失败,只有最后一个条件返回true65 $template = get_index_template();
这是我们的最终回退,本质上是返回和加载index.php
正如你所见,如果你index.php
在您的主题或每个类别的模板中,WordPress会在每次加载页面时完成上述所有工作。因此,只有index.php
或每个类别的类别页。
由于模板加载器需要使用更多模板,因此加载时间会略微(与实际情况不相关(我们在这里讨论的是非常小的增加)受到影响,但这绝不会是一个问题(除非您有数百个模板)
。。。使用条件标记。。。。这是否会被视为不良行为?
正如我在链接答案中所述,你只需要index.php
为了在站点上显示任何页面,为了方便起见,可以使用任何其他模板。您只能index.php
如果愿意,可以使用条件标记来针对特定的页面类型。
关于这个问题,实际上没有标准,这是用户的偏好,以及模板的可读性和可维护性。如果只需要更改页面之间的标题,可以在中使用条件句index.php
, 如果需要更多更改,最好为特定页面创建特定模板。只是index.php
有数百个条件和几百行代码。这样的文件会非常混乱,由于需要检查数百个条件,加载可能需要更长的时间
如果两种方法都有效,是否会对加载速度产生任何影响?哪一个更快?
页面速度在这里真的不应该是一个问题,因为我已经在前面的所有内容中显示了这一点。选择这两个选项中的任何一个可能对页面加载时间有任何影响,也可能不会有任何影响。即使对页面加载时间有影响,差异也几乎无法衡量。
这里真正应该决定的因素是模板的可维护性和可用性,这些模板的可读性和主题的总体用户友好性,以及您的偏好和您的习惯。
我希望这能对整个问题有所启发
是否有任何内置方法可以根据浏览器大小显示不同的模板(即移动设备检测)?我做了一些研究,我能找到的只是大量插件,它们的功能远远超出了我的需要。我基本上只需要一种方法,将移动目录添加到我的主题中,并为移动用户显示该主题。