spot_img
HomeWordPress教程wordpress建站公司 如果我将 index.php 用作 HTML 线框与将每个主模板文件编写为完整的 HTML 文档有什么区别?

wordpress建站公司 如果我将 index.php 用作 HTML 线框与将每个主模板文件编写为完整的 HTML 文档有什么区别?

JasperAI 10000字免费额度试用

假设一个页面页眉和页脚元素在每个视图中都保持不变的网站:由于 WP 的模板层次结构系统,我可以在 index.php 中对一般网站布局进行硬编码。 然后,我可以在 index.php 中使用条件标记来确定要呈现哪些内部内容模板。

另一方面,我可以在每个主要模板中重复编写基本布局。 因此,我会写更多的代码行并引入更多的冗余。 但是,也许我可以保存一些文件 I/O 操作,如果可以完全呈现​​ single.php 而不必回退到 index.php?

选项1 没有 single.php、category.php、archive.php 等等,但有 content-post.php、content-category.php …

<?php
/**
 * The index.php file
 *
 */
get_header();
get_template_part('partials/layout-block-01');
get_template_part('partials/layout-block-02');
if (have_posts()) : 
  if (is_front_page()){
    ... // some front page specific markup and settings
  }
  elseif(is_category()){
    ... // some category specific markup and settings
  }
  elseif (is_singular()) {
    ... // some singular post specific markup and settings
  }
  elseif (is_archive()) {
    ... // some general archive specific markup and settings
  }
  else{
    ... // defaults
  }
  while (have_posts()) : 
    the_post();
    get_template_part( 'content', get_post_format());
  endwhile;
endif;
get_footer();
?>

选项 2: 每个主模板文件都包含全部,像这样

<?php
/**
 * The single.php file
 *
 */
get_header();
get_template_part('partials/layout-block-01');
get_template_part('partials/layout-block-02');
if (have_posts()) : 
  while (have_posts()) : 
    the_post();
    ... // single post markup
  endwhile;
endif;
get_footer();
?>

和另一个主要模板文件

<?php
/**
 * The category.php file
 *
 */
get_header();
get_template_part('partials/layout-block-01');
get_template_part('partials/layout-block-02');
if (have_posts()) : 
  while (have_posts()) : 
    the_post();
    ... // category archive markup
  endwhile;
endif;
get_footer();
?>

那么,这两种选择都涉及性能问题吗? 这只是个人品味的问题吗?

选项 2 是最佳选项。 要知道为什么,需要查看模板加载器。

(模板层次结构没有任何意义,如果你不知道它是如何工作的或来自)

    if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) :
59          $template = false;
60          if     ( is_404()            && $template = get_404_template()            ) :
61          elseif ( is_search()         && $template = get_search_template()         ) :
62          elseif ( is_front_page()     && $template = get_front_page_template()     ) :
63          elseif ( is_home()           && $template = get_home_template()           ) :
64          elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
65          elseif ( is_tax()            && $template = get_taxonomy_template()       ) :
66          elseif ( is_attachment()     && $template = get_attachment_template()     ) :
67                  remove_filter('the_content', 'prepend_attachment');
68          elseif ( is_single()         && $template = get_single_template()         ) :
69          elseif ( is_page()           && $template = get_page_template()           ) :
70          elseif ( is_singular()       && $template = get_singular_template()       ) :
71          elseif ( is_category()       && $template = get_category_template()       ) :
72          elseif ( is_tag()            && $template = get_tag_template()            ) :
73          elseif ( is_author()         && $template = get_author_template()         ) :
74          elseif ( is_date()           && $template = get_date_template()           ) :
75          elseif ( is_archive()        && $template = get_archive_template()        ) :
76          elseif ( is_comments_popup() && $template = get_comments_popup_template() ) :
77          elseif ( is_paged()          && $template = get_paged_template()          ) :
78          else :
79                  $template = get_index_template();
80          endif;

无论如何,此模板加载器都会在前端的每个页面加载上运行。 如您所见,WordPress 遍历一个列表并根据请求的页面验证条件。 让我们以类别页面为例,类别是 未分类 类别。

高质量外链购买

if/else 语句执行返回 true 的第一个条件,在类别页面上它将是 is_category() 条件语句。 该语句执行 get_category_template() 并评估该声明。 如果这个声明也成立(有效的类别页面可用), 模板根据找到的内容加载,如果此语句返回 false,模板加载器继续寻找下一个 true 语句,稍后我将解决这个问题。

里面发生了什么 get_category_template() 很重要。 该函数根据请求的类别页面创建三个模板名称。 例如,按顺序创建以下内容, category-uncategorized.php, category-1.phpcategory.php. 此模板名称存储在数组中并传递给 get_query_template(). 该函数的任务是搜索传递给它的模板名称并返回第一个存在且可用的模板。 这是用 locate_template().

所以这就是说,WordPress(实际上 locate_template() 如果你想获得技术) 从寻找开始 category-uncategorized.php,如果找到,则返回并加载模板。 如果找不到,WordPress 会继续尝试 category-1.php 如果失败,它最后会尝试 category.php. 如果没有找到,就像我说的,我们的

elseif ( is_category()       && $template = get_category_template()  

condition 返回 false 并评估进一步向下的条件。 下一个正确的条件是 is_archive() (记住, is_archive() 在类别、标签、分类法、自定义帖子类型档案、作者和日期相关页面上返回 true). 发生与上述相同的过程,但这次是 archive.php 正在寻找并加载(如果存在)。 如果 archive.php 不存在,整个条件语句评估为加载的默认值 index.php 通过 get_index_template(). 这是每个页面的最终回退

结论和最终答案

即使你可能认为拥有 category.php 会导致很大的开销,它不会。 到那个时候 index.php 已加载到您的类别页面,(让我们再次使用我们的示例) 为了 未分类 类别,WordPress 已经查看并尝试加载 category-uncategorized.php, category-1.php, category.phparchive.php. 所以继续并拥有你的 nice-to-have 模板 (所有模板除外 index.php)(这是你的 选项 2). 拥有 10 个可有可无的模板总比一个 index.php 它和尼罗河一样长,里面塞满了比爱因斯坦脑子里更多的逻辑(这是你的 选项1)

这也很重要,回到 选项1 你保持模板尽可能短,可维护和可读。 有一个模板真的没有用,它可能会快 0.0000001 秒,但是一团糟,维护起来真是一场噩梦。

最后,选项 2 比选项 1 有更多的优点。

保存文件 I/O 操作.
如果您愿意,您可以在该级别上尽可能多地保存,但最终所有内容都将出现在您的编码/服务器配置等方面。
静态/动态文件也可以/应该/应该被缓存(可能在服务器级别或通过插件/自己的代码)并且如果它是关于文件“充满代码”或数量的小事,这也将帮助您减少担心模板。

模板还原

关于减少模板使用的方法,并不总是“少即是多”。
这是另一个例子(也被 ialocin 提到):
使用标准循环(对于 front-page.php/index.php/page.php/single.php 当然还有其他模板)

<?php
if ( have_posts() ) : 

    // Start the Loop.
    while ( have_posts() ) : the_post();

    // Include format-specific template for the content.
    get_template_part( 'content', get_post_format() );

    // End the loop.
    endwhile;
else :
    // If no content, include the "No posts found" template part.
    get_template_part( 'content', 'none' );

endif;
?>

并将此示例用于 content.php (应该在主题文件夹中)

WordPress建站服务
 <?php
 if ( is_front_page() ) { get_template_part( 'include/content', 'front' ); }
 if ( is_single() ) { get_template_part( 'include/content', 'single' ); }
 if ( is_page() ) { get_template_part( 'include/content', 'page' ); }
// and so on
?>

并且(在包含文件夹中)你可以使用像这样的文件 content-front.php / content-single.php 等等。

这些 template_parts (这将包含在 loop,如上述命名文件中所述)包含您希望这些模板文件拥有的代码。

以供参考:

循环
模板层次结构
获取模板部分
组织主题文件
页面模板

这种方法 似乎 只犯一个小错误的网站“不太敏感”。 因为 ppl 将主要在模板部分(在该子文件夹中)工作。 当然,一切都取决于错误的类型,但我想你知道这是什么意思。

恕我直言,这种方式是否也更快/更容易触发错误并且更容易参考。

5分钟生成10篇英文软文article forge软件试用
siteground guangda
WordPress花园官方账号
WordPress花园隶致力于为广大跨境电商和独立站爱好者提供优质的WordPress教程、Woocommerce教程、Facebook、Twitter、tiktok、Instagram教程和谷歌SEO教程等资料和对应的建站推广服务。关注‘哟派出海’公众号了解最新资讯。粉丝福利:Shopline免费独立站建设14天优惠 商务合作: [email protected]
RELATED ARTICLES
spot_img