WordPress模板层次结构初学者指南(备忘单)

After our infographic on how WordPress works behind the scenes, several users asked us to cover how a WordPress theme works behind the scenes.

在获得有关WordPress在幕后如何工作的信息图之后,一些用户要求我们介绍WordPress主题在幕后如何工作。

In this article, we will explain the WordPress template hierarchy for beginners. This cheat sheet is extremely useful when customizing a template or creating a custom WordPress theme.

在本文中,我们将为初学者解释WordPress模板层次结构。 该备忘单在自定义模板或创建自定义WordPress主题时非常有用。

WordPress template hierarchy explained for beginners
为什么要了解WordPress模板层次结构? (Why Learn About WordPress Template Hierarchy?)

All modern WordPress themes consist of templates, stylesheets, javascript, and images. Together these files control how your site looks to the users.

所有现代WordPress主题均由模板,样式表,javascript和图像组成。 这些文件一起控制着网站对用户的外观。

Related: 9 things you must look for when selecting the perfect WordPress theme for your site.

相关:为您的网站选择完美的WordPress主题时必须寻找的9件事。

WordPress has a standard template hierarchy. This means that templates with certain filenames affect specific areas on your website. It also tells you what template name to use for a specific item.

WordPress具有标准的模板层次结构。 这意味着具有某些文件名的模板会影响您网站上的特定区域。 它还告诉您用于特定项目的模板名称。

Most modern WordPress themes come with templates to display category, date, archives, single posts, custom pages, and more. As a user, you can create more templates by creating a child theme.

大多数现代WordPress主题都带有用于显示类别,日期,档案,单个帖子,自定义页面等的模板。 作为用户,您可以通过创建子主题来创建更多模板。

Having said that, let’s take a look at how this WordPress template hierarchy works behind the scenes.

话虽如此,让我们看看这个WordPress模板层次结构如何在后台工作。

可视化WordPress模板层次结构(备忘单) (Visualizing WordPress Template Hierarchy (Cheat Sheet))

WordPress uses an easy to understand and meaningful pattern for template names. The visual cheat sheet below explains which template files are used to display different pages on a WordPress site.

WordPress对模板名称使用了易于理解且有意义的模式。 下面的可视备忘单解释了哪些模板文件用于在WordPress网站上显示不同的页面。

Here is a breakdown of which WordPress template files to edit for different pages in a typical WordPress site.

以下是在典型WordPress网站中针对不同页面编辑的WordPress模板文件的分类。

主页使用哪些模板文件? (Which template files are used by home page?)

Site front page

Out of the box, WordPress displays your blog posts on the home page of your website. You can also set it to use a custom home page (also known as front page) by visiting Settings » Reading page in WordPress admin area.

WordPress开箱即用,在您的网站首页上显示您的博客文章。 您还可以通过访问WordPress管理区域中的“设置”»“阅读”页面,将其设置为使用自定义首页(也称为首页)。

1. front-page.php – If you are using a static front page, then WordPress will first look for this template file and use it to display front page.

1. front-page.php –如果您使用的是静态首页,则WordPress首先会查找此模板文件并使用它显示首页。

2. home.php – If you are using static front page, and you don’t have front-page.php template in your theme, then WordPress will look for home.php template. It is also used to display default blog posts on homepage.

2. home.php –如果您使用的是静态首页,并且主题中没有front-page.php模板,那么WordPress将寻找ho​​me.php模板。 它还用于在主页上显示默认博客文章。

3. index.php – If front-page.php or home.php do not exist, then WordPress falls back to index.php template to display homepage. This template is the default fallback template in WordPress to display any page.

3. index.php –如果不存在front-page.php或home.php,则WordPress将退回到index.php模板来显示主页。 此模板是WordPress中显示任何页面的默认后备模板。

单个帖子使用哪些模板文件? (Which template files are used by single post?)

Single post page template

WordPress looks for these files to display a single post.

WordPress查找这些文件以显示单个帖子。

1. single-{post-type}-{slug}.php – Use this template to modify the display of a specific individual post in any post type. For example, if post type is ‘review’ and the post slug is acme-phone, then WordPress would look for single-review-acme-phone.php.

1. single- {post-type}-{slug} .php –使用此模板可以修改任何帖子类型中特定单个帖子的显示。 例如,如果帖子类型为'review',而帖子为acme-phone,则WordPress将查找single-review-acme-phone.php

2. single-{post-type}.php – WordPress will then check if there is a template to display this specific post type. For example, if the post type is review, then WordPress would look for single-review.php.

2. single- {post-type} .php – WordPress将检查是否有显示此特定帖子类型的模板。 例如,如果帖子类型为评论,则WordPress将寻找single-review.php

3. single.php – WordPress will then fall back to single.php.

3. single.php –然后WordPress将退回到single.php

4. singular.php – This template adds another fallback to display a single item from any post type.

4. singular.php –此模板添加了另一个后备,以显示任何帖子类型的单个项目。

5. index.php – Finally, as mentioned above, WordPress ultimately falls back to index.php.

5. index.php –最后,如上所述,WordPress最终退回到index.php。

单页使用哪些模板文件? (Which template files are used by single page?)

Static page template

WordPress pages are one of the default post types. They allow you to create static pages in your website instead of posts. See our guide on the difference between posts vs pages.

WordPress页面是默认的帖子类型之一。 它们使您可以在网站中创建静态页面,而不是帖子。 有关帖子与页面之间的区别,请参见我们的指南。

1. Custom Page Template – The page template assigned to the page. See how to create a custom page template in WordPress.

1.自定义页面模板 –分配给页面的页面模板。 了解如何在WordPress中创建自定义页面模板

2. page-{slug}.php – If the page slug is contact-us, WordPress will look to use page-contact-us.php.

2. page- {slug} .php –如果page slug是contact-us,则WordPress将使用page-contact-us.php

3. page-{id}.php – If the page ID is 17, then WordPress will look for a template file named page-17.php.

3. page- {id} .php –如果页面ID为17,则WordPress将寻找名为page-17.php的模板文件。

4. page.php – The template to display all static pages.

4. page.php –显示所有静态页面的模板。

5. singular.php – This template is a default fallback to all single post type items.

5. singular.php –该模板是所有单个帖子类型项目的默认后备。

6. index.php – The default fallback template.

6. index.php –默认的后备模板。

类别档案使用哪些模板文件? (Which template files are used by category archives?)

Category archive template

WordPress uses these files to display category related pages in WordPress.

WordPress使用这些文件来显示WordPress中与类别相关的页面。

1. category-{slug}.php – This template is used to display category archive page for a specific category. For example, if category slug is reviews, then WordPress will look for category-reviews.php template.

1. category- {slug} .php –此模板用于显示特定类别的类别存档页面。 例如,如果category slug是reviews,则WordPress将寻找category-reviews.php模板。

2. category-{id}.php – WordPress then looks for a template with category ID. For example, if category ID is 17, then WordPress will look for category-17.php.

2. category- {id} .php –然后WordPress寻找带有类别ID的模板。 例如,如果类别ID为17,则WordPress将查找category-17.php

3. category.php – This is the default template to display all category archive pages in WordPress.

3. category.php –这是默认模板,用于显示WordPress中的所有类别存档页面。

4. archive.php – This is the default template used by WordPress to display any archive pages.

4. archive.php –这是WordPress用于显示任何存档页面的默认模板。

5. index.php – The default fallback template.

5. index.php –默认的后备模板。

标签归档使用哪些模板文件? (Which template files are used by tag archives?)

Tag archive template

WordPress uses these files to display tag archive pages.

WordPress使用这些文件来显示标签存档页面。

1. tag-{slug}.php – If the tag’s slug is fruits, WordPress will look for tag-fruits.php.

1. tag- {slug} .php –如果标签的标签是水果,则WordPress将寻找tag-fruits.php

2. tag-{id}.php – If the tag’s ID is 17, WordPress will look for tag-17.php template.

2. tag- {id} .php –如果标签的ID为17,WordPress将寻找tag-17.php模板。

3. tag.php – The default template for tag archives.

3. tag.php –标签归档的默认模板。

4. archive.php – The default template for any achive page.

4. archive.php –任何访问页面的默认模板。

5. index.php – The default fallback template.

5. index.php –默认的后备模板。

定制分类档案使用哪些模板文件? (Which template files are used by custom taxonomy archives?)

Custom taxonomy archive

Categories and tags are two default WordPress taxonomies. Users can also create their own custom taxonomies as well. Here is how WordPress looks for templates to display custom taxonomy pages.

类别和标签是两种默认的WordPress分类法。 用户还可以创建自己的自定义分类法 。 这是WordPress寻找显示自定义分类页面的模板的方式。

1. taxonomy-{taxonomy}-{term}.php – If you have a custom taxonomy called genre, and there is a term ‘thriller’, then WordPress will look for taxonomy-genre-thriller.php.

1. taxonomy- {taxonomy}-{term} .php –如果您有一个名为genre的自定义分类法,并且有一个术语'thriller',那么WordPress将寻找taxonomy-genre-thriller.php

2. taxonomy-{taxonomy}.php – If the taxonomy were genre, WordPress would look for taxonomy-genre.php.

2. taxonomy- {taxonomy} .php –如果分类法是流派,WordPress将寻找taxonomy-genre.php

3. taxonomy.php – The default template to display any custom taxonomy archives.

3. taxonomy.php –显示所有自定义分类档案的默认模板。

4. archive.php – The default fallback for all archive pages in WordPress.

4. archive.php – WordPress中所有存档页面的默认后备。

5. index.php– The default fallback template in WordPress.

5. index.php – WordPress中的默认回退模板。

自定义帖子类型使用哪些模板文件? (Which template files are used by custom post types?)

Custom post type archive

Here is how WordPress looks for templates to display custom post type archives.

这是WordPress寻找显示自定义帖子类型档案的模板的方式。

1. archive-{post_type}.php – If you have a post type is review, WordPress will look for archive-review.php.

1. archive- {post_type} .php –如果您的帖子类型为评论,则WordPress将查找archive-review.php

2. archive.php – The default template to display all archive pages in WordPress.

2. archive.php –默认模板,用于显示WordPress中的所有存档页面。

3. index.php – The default fallback template in WordPress.

3. index.php – WordPress中的默认回退模板。

哪些模板文件用于显示作者档案? (Which template files are used to display author archives?)

Author archive

WordPress generates archive pages for each author on your WordPress site. Here is how it looks for author archive template.

WordPress会为您的WordPress网站上的每个作者生成存档页面。 这是寻找作者档案模板的方式。

1. author-{nicename}.php – If the author’s nice name is matt, WordPress will look for author-matt.php.

1. author- {nicename} .php –如果作者的好名字是matt,则WordPress将查找author-matt.php。

2. author-{id}.php – If the author’s user ID is 6, then WordPress will look for author-6.php.

2. author- {id} .php –如果作者的用户ID为6,则WordPress将查找author-6.php

3. author.php – The default template used to display author archive pages in WordPress.

3. author.php –用于在WordPress中显示作者存档页面的默认模板。

4. archive.php – The default template to display all archive pages in WordPress.

4. archive.php –默认模板,用于显示WordPress中的所有存档页面。

5. index.php – The default fallback template in WordPress.

5. index.php – WordPress中的默认回退模板。

哪些模板文件用于显示基于日期的存档? (Which template files are used to display date based archives?)

Date based archive

WordPress also displays your posts on date based archive pages for months and years. Here is how it looks for templates for these pages.

WordPress还会在基于日期的存档页面上显示您的帖子数月和数年。 这是这些页面的模板查找方式。

1. date.php – The default template for date based archives.

1. date.php –基于日期的存档的默认模板。

2. archive.php – The default template used to display author archive pages in WordPress.

2. archive.php –用于在WordPress中显示作者存档页面的默认模板。

3. index.php – The default fallback template in WordPress.

3. index.php – WordPress中的默认回退模板。

哪些模板文件用于显示搜索页面? (Which template files are used to display search pages?)

Search result page

1. search.php – The default page to display search results in WordPress.

1. search.php –在WordPress中显示搜索结果的默认页面。

2. searchform.php – The template to display a search form in WordPress.

2. searchform.php –在WordPress中显示搜索表单的模板。

3. index.php – The default fallback template in WordPress.

3. index.php – WordPress中的默认回退模板。

哪些模板文件用于显示404错误页面? (Which template files are used to display 404 error pages?)

404 Error page

The 404 error page is displayed when WordPress is unable to find the requested content. See our guide on how to improve your 404 page template.

当WordPress无法找到请求的内容时,将显示404错误页面。 请参阅有关如何改进404页面模板的指南

1. 404.php – The default template to display 404 error page in WordPress.

1. 404.php –在WordPress中显示404错误页面的默认模板。

2. index.php – The default fallback template in WordPress.

2. index.php – WordPress中的默认回退模板。

哪些模板文件用于显示附件页面? (Which template files are used to display attachment pages?)

Attachment pages

1. MIME_type.php – Mime_type stands for file type. For example, image.php, video.php, application.php.

1. MIME_type.php – Mime_type代表文件类型。 例如,image.php,video.php,application.php。

2. attachment.php – The default template to display attachment pages.

2. attachment.php –显示附件页面的默认模板。

3. single-attachment.php – To display a single attachment.

3. single-attachment.php –显示单个附件。

4. single.php – The default template to display single post type items.

4. single.php –显示单个帖子类型项目的默认模板。

5. index.php – The default fallback template in WordPress.

5. index.php – WordPress中的默认回退模板。

哪些模板文件用于显示嵌入? (Which template files are used to display embeds?)

Embeds

Since WordPress 4.5, you can use templates to render a post embedded into WordPress.

WordPress 4.5开始 ,您可以使用模板来呈现嵌入WordPress的帖子。

1. embed-{post-type}-{post_format}.php – WordPress will look for a post type and post format template first. For example, if you have a review with video, then WordPress will look for embed-review-video.php.

1. embed- {post-type}-{post_format} .php – WordPress将首先查找帖子类型和帖子格式模板。 例如,如果您对视频进行评论,则WordPress将寻找embed-review-video.php

2. embed-{post-type}.php – If the post type is review, WordPress would look for embed-review.php.

2. embed- {post-type} .php –如果帖子类型为评论,WordPress将寻找embed-review.php

3. embed.php – The default fallback for all embeds.

3. embed.php –所有嵌入的默认回退。

We hope this tutorial helped you learn about the WordPress template hierarchy. You may also want to see our list of the best drag & drop WordPress page builders.

我们希望本教程可以帮助您了解WordPress模板层次结构。 您可能还希望查看我们的最佳拖放WordPress页面构建器列表

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-themes/wordpress-template-hierarchy-explained/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值