首页 » 后端 » Wordpress » 正文

[转载] wp主题文件执行顺序(E文)

发布者:站点默认
2012/03/20 浏览数(100,684) 分类:Wordpress [转载] wp主题文件执行顺序(E文)已关闭评论

说明:

本文转自 Avinash 的《The WordPress Theme Files Execution Hierarchy》,国人翻译版可查看 荒野无灯 的《WordPress主题文件的执行顺序及其层次结构》。

正文

This article will show the WordPress theme file execution hierarchy. In short, we’ll look at which files get served up when you load a page in WordPress. You might already know that detail post is served by single.php and detail page is served by page.php, but WordPress will search for different files depending on a variety of factors, so we’ll be looking at how this works!

First thing we should establish is this: without index.php and style.css your theme is no longer a valid WordPress theme… so it stands to reason that if all you have is those two files, each page will you try to load will be served up by index.php. Take a quick peek at this “cheatsheet” to see what I’m referring to:

WP CheatSheet TemplateMap

WP CheatSheet TemplateMap

wordpress主题结构层次图(上图的另一个版本)

wordpress主题结构层次图(上图的另一个版本)

Notice that the flow for each page types will end up with the index.php. That is the reason why index.php is required file for the WordPress theme. If we are missing any other files in WordPress theme (for instance, if there is no “search.php” file included in the theme), then index.php will be served instead.

Now let’s look at some details about the execution order. I am going to show you the flow in which WordPress will search for files in your active theme folder. I hope this will be useful when you create a WordPress theme from now on:

I will go through each type of files one by one and will show the execution hierarchy for the same.

Home Page

This is the first and most important page of any website. So WordPress has provided the scope to customize the page. Let’s have a look at the file hierarchy for the home page.

front-page.php
home.php
index.php

While serving the home page, WordPress will search for front-page.php. If that is not found, it will use home.php. If home.php exists, it’ll use that. If not, it will simply default to using index.php.

WordPress Post Detail

single-[post-type].php
single.php
index.php

WordPress can have as many post types as we need. So this will be easier to get different design for all/some post types. By default ‘post’ is the main and default post type of the WordPress.

So for example, if your custom post type is product then it will be single-product.php

To know more how to add new post types in WordPress you can refer to this link.

WordPress Page Detail

[custom-template].php
page-[slug].php
page-[id].php
page.php
index.php
custom page template

custom page template

Just the same as with post types, we can have a different page layout using the custom page template. So WordPress first searches for the files of the selected Page template (if it exists).

If none are found, it will search for the file with the slug of the current page. Basically, if the slug is aboutus, then it will search for the file page-aboutus.php in active theme folder.

WordPress will search for the files with the ID just like searching for the files with slug.

Category Page

category-[slug].php
category-[id].php
category.php
archive.php
index.php

From the above flow, you can understand that how you can have different templates used for the category page. For instance, you could have a custom page based on slug and id, and then use a default “category.php” file for the rest of your categories..

Tag Page

tag-[slug].php
tag-[id].php
tag.php
archive.php
index.php

This will be same case as the category. You can have different pages for tag slug and tag id also.

Taxonomy Page

taxonomy-[tax]-[term].php
taxonomy-[tax].php
taxonomy.php
archive.php
index.php

Here goes the different file hierarchy for the taxonomy Pages.

Author Page

author-[author-nicname].php
author-[author-id].php
author.php
archive.php
index.php

Here you come to know that you can have different designs based on users also. Same as category and tags we can have different files based on slug and ID of the user.

Attachment Page

[mime-type].php
attachment.php
single.php
index.php

Here you can see that you can have different page layout for different types of attachment. These can be differentiate from the mime type of the attached file.

Date Page

date.php
archive.php
index.php

For the date specific layout we can create date.php in theme folder. Then the flow goes to archive.php and then at last index.php.

Archive Page

archive.php
index.php

As we come downwards to the type of files, number of files are reduced in the hierarchy. So this are the basic or we can say most used files in any WordPress themes.

Search Page

search.php
index.php

You can customize your search result with the search.php first. If search.php is not available then index.php will be served.

404 Page

404.php
index.php

In the case of page or post not found, WordPress will search for 404.php then if not found then it will serve index.php.

Conclusion

You can obviously use this information in a wide range of ways to load up custom templates for various pages… In many cases, even if you’re using an existing theme, you can get a custom solution without modifying the existing files. You will just need to create new file and give it a new name using the information above.
Share your thoughts and any additional file which can be included above hierarchy.

点击返回顶部
  1. 留言
  2. 联系方式