WordPress模板文件和对应函数概要

主页显示

默认情况下,WordPress会设置您网站的主页以显示您的最新博客文章。此页面称为博客文章索引。您还可以将博客文章设置为显示在单独的静态页面上。home.php无论是用作首页还是单独的静态页面,模板文件均用于呈现博客文章索引。如果 home.php不存在,WordPress将使用 index.php

  1. home.php
  2. index.php

❗️如果front-page.php 存在,它将覆盖 home.php 模板。

首页显示

front-page.php无论首页显示博客文章索引(如上所述)还是静态页面,模板文件均用于呈现网站的首页。主页模板优先于博客文章索引(home.php)模板。如果front-page.php文件不存在,WordPress将根据设置→阅读中的设置使用home.php或page.php文件。如果这些文件都不存在,它将使用该index.php文件。

  1. front-page.php –用于“ 设置”→“阅读” 首页显示部分中设置的“ 您的最新帖子 ”或“ 静态页面 ” 。
  2. home.php –如果WordPress无法找到, front-page.php并且在首页显示部分中设置了“ 您的最新帖子 ” ,它将查找。此外,当在首页显示部分中设置帖子页面时,WordPress会查找此文件。home.php
  3. page.php –在首页显示区域中设置“ 首页 ”时。
  4. index.php –在首页显示区域中设置“ 您的最新帖子 ” 但 不存在时,或 在首页中设置但不存在时。home.phppage.php

如您所见,WordPress采取的路径有很多规则。使用上面的图表是确定WordPress将显示什么的最佳方法。

隐私权政策页面显示

privacy-policy.php模板文件是用来提供您网站的隐私政策页。隐私策略页面模板优先于静态页面(page.php)模板。如果privacy-policy.php文件不存在,WordPress将根据可用模板使用page.phpsingular.php文件。如果这些文件都不存在,它将使用该index.php文件。

  1. privacy-policy.php–用于设置→隐私的更改您的隐私政策页面部分中设置的隐私政策页面。
  2. custom template file– 分配给页面的页面模板。请参阅get_page_templates()。
  3. page-{slug}.php–如果页面上没有标签privacy,WordPress将使用page-privacy.php
  4. page-{id}.php–如果页面ID为6,WordPress将使用page-6.php。
  5. page.php
  6. singular.php
  7. index.php

单帖

单个帖子模板文件用于呈现单个帖子。WordPress使用以下路径:

  1. single-{post-type}-{slug}.php –(自4.4开始)首先,WordPress为特定帖子寻找模板。例如,如果帖子类型为product,而帖子标签为dmc-12,则WordPress将查找single-product-dmc-12.php
    2.single-{post-type}.php–如果帖子类型为product,WordPress将寻找single-product.php
    3.single.php–然后WordPress退回到single.php
    4.singular.php–然后回落到singular.php
  2. index.php–最后,如上所述,WordPress最终回退到index.php

单页

用于呈现静态页面的模板文件(pagepost-type)。请注意,与其他帖子类型不同,pageWordPress是专用的,并使用以下路径:

custom template file – 分配给页面的页面模板。请参阅get_page_templates()。

  1. page-{slug}.php –如果页面上没有标签recent-news,WordPress将使用page-recent-news.php。
  2. page-{id}.php –如果页面ID为6,WordPress将使用page-6.php
  3. page.php
  4. singular.php
  5. index.php

分类

呈现类别存档索引页面在WordPress中使用以下路径:

  1. category-{slug}.php –如果类别的子类别为news,WordPress将寻找category-news.php
  2. category-{id}.php –如果类别的ID为6,WordPress将寻找category-6.php
  3. category.php
  4. archive.php
  5. index.php

标签

为了显示标签存档索引页面,WordPress使用以下路径:

  1. tag-{slug}.php –如果标签的子标签为sometag,WordPress将寻找tag-sometag.php
  2. tag-{id}.php –如果标签的ID为6,WordPress将寻找tag-6.php
  3. tag.php
  4. archive.php
  5. index.php

自定义分类法

自定义分类法使用的模板文件路径略有不同:

1.taxonomy-{taxonomy}-{term}.php -如果分类学sometax,和分类的任期someterm,WordPress会寻找taxonomy-sometax-someterm.php.在的情况下,后期格式,分类标准是**“post_format**”和术语是“后格式- {}格式。即taxonomy-post_format-post-format-link.php 用于链接发布格式。

  1. taxonomy-{taxonomy}.php –如果是分类法sometax,WordPress将寻找taxonomy-sometax.php
  2. taxonomy.php
  3. archive.php
  4. index.php

自定义帖子类型

自定义帖子类型使用以下路径来呈现适当的存档索引页面。

1.archive-{post_type}.php –如果帖子类型为product,WordPress将寻找archive-product.php
2. archive.php
3. index.php

作者显示

根据以上示例,呈现作者档案索引页面是相当说明性的:

  1. author-{nicename}.php –如果作者的好名字是matt,WordPress将寻找author-matt.php
  2. author-{id}.php –如果作者的ID为6,WordPress将寻找author-6.php
  3. author.php
  4. archive.php
  5. index.php

日期

基于日期的存档索引页面将按您期望的方式呈现:

  1. date.php
  2. archive.php
  3. index.php

搜索结果

搜索结果遵循与其他模板类型相同的模式:

  1. search.php
  2. index.php

404(未找到)

同样,按以下顺序调用404模板文件:

  1. 404.php
    2.index.php

附件

呈现附件页面(attachment帖子类型)使用以下路径:

  1. {MIME-type}.php-可以是任何的MIME类型(例如: image.php,video.php,pdf.php)。对于text/plain,使用以下路径(按顺序):
  2. text-plain.php
  3. plain.php
  4. text.php
  5. attachment.php
  6. single-attachment-{slug}.php –例如,如果附件为holiday,WordPress将寻找single-attachment-holiday.php。
  7. single-attachment.php
  8. single.php
  9. singular.php
  10. index.php

嵌入

嵌入模板文件用于呈现正在嵌入的帖子。从4.5开始,WordPress使用以下路径:

  1. embed-{post-type}-{post_format}.php –首先,WordPress寻找特定帖子的模板。例如,如果其发布类型为post且具有音频格式,则WordPress将寻找embed-post-audio.php。
  2. embed-{post-type}.php–如果帖子类型为product,WordPress将寻找embed-product.php。
  3. embed.php–然后WordPress退回嵌入.php。
  4. 最后,WordPress最终会退回到其自己的wp-includes/theme-compat/embed.php模板。

非ASCII字符处理

下面是一个名为的“Hello World😄”用的ID页面的页面模板层次6:

page-hello-world-😄.php
page-hello-world-%f0%9f%98%80.php
page-6.php
page.php
singular.php
相同的行为适用于帖子,术语名称和作者好记号。

过滤层次结构

WordPress模板系统可让您过滤层次结构。这意味着您可以在层次结构的特定点插入和更改内容。过滤器(位于get_query_template()函数中)使用以下过滤器名称:模板类型"{KaTeX parse error: Expected 'EOF', got '}' at position 5: type}̲_template"在哪里type。

以下是模板层次结构中所有可用过滤器的列表:

  • embed_template
  • 404_template
  • search_template
  • frontpage_template
  • home_template
  • privacypolicy_template
  • taxonomy_template
  • attachment_template
  • single_template
  • page_template
  • singular_template
  • category_template
  • tag_template
  • author_template
  • date_template
  • archive_template
  • index_template

Example

例如,让我们采用默认的作者层次结构:

  • author-{nicename}.php
  • author-{id}.php
  • author.php
    要添加author-{role}.php之前author.php,我们可以使用’author_template’模板类型来操纵实际的层次结构。这允许对/ author / username的请求,其中,如果用户名在当前主题目录中存在,则用户名具有编辑者的角色,可以使用author-editor.php显示。
function author_role_template( $templates = '' ) { 
    $author = get_queried_object(); 
    $role = $author->roles[0]; 
    if ( ! is_array( $templates ) && ! empty( $templates ) ) { 
        $templates = locate_template( array( "author-$role.php", $templates ), false ); 
    } elseif ( empty( $templates ) ) { 
        $templates = locate_template( "author-$role.php", false ); 
    } else { 
        $new_template = locate_template( array( "author-$role.php" ) ); 
        if ( ! empty( $new_template ) ) { 
            array_unshift( $templates, $new_template ); 
        } 
    } 
    return $templates; 
} 
add_filter( 'author_template', 'author_role_template' );
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值