템플릿 파일 계층 구조

일반적인 아이디어

WordPress uses the Query String — information contained within each link on your web site — to decide which template or set of templates will be used to display the page.

First, WordPress matches every Query String to query types — i.e. it decides what type of page (a search page, a category page, the home page etc.) is being requested.

Templates are then chosen — and web page content is generated — in the order suggested by the WordPress Template hierarchy, depending upon what templates are available in a particular WordPress Theme.

WordPress looks for template files with specific names in the current Theme's directory and uses the first matching template file listed under the appropriate query section below.

With the exception of the basic index.php template file, Theme developers can choose whether they want to implement a particular template file or not. If WordPress cannot find a template file with a matching name, it skips down to the next file name in the hierarchy. If WordPress cannot find any matching template file, index.php (the Theme's home page template file) will be used.

예

If your blog is at http://example.com/blog/ and a visitor clicks on a link to a category page like
http://example.com/blog/category/your-cat/: Here is the progression of how WordPress uses the template hierarchy to find and generate the right file.

WordPress looks for a template file in the current Theme's directory that matches the category's ID.

If the category's ID is 4, WordPress looks for a template file named category-4.php.

If it is missing, WordPress next looks for a generic category template file, category.php.

If this file does not exist either, WordPress looks for a generic archive template, archive.php.

If it is missing as well, WordPress falls back on the main Theme template file, index.php.

If a visitor goes to your home page at http://example.com/blog/, the following happens:

페이지 표시

page-{slug}.php - If the page slug is recent-news, WordPress will look to use page-recent-news.php

page-{id}.php - If the page ID is 6, WordPress will look to use page-6.php

page.php

index.php

카테고리 표시

category-{slug}.php - If the category's slug were news, WordPress would look for category-news.php

category-{id}.php - If the category's ID were 6, WordPress would look for category-6.php

category.php

archive.php

index.php

태그 표시

tag-{slug}.php - If the tag's slug were sometag, WordPress would look for tag-sometag.php

tag-{id}.php - If the tag's ID were 6, WordPress would look for tag-6.php

tag.php

archive.php

index.php

사용자정의 분류 표시

taxonomy-{taxonomy}-{slug}.php - If the taxonomy were sometax, and taxonomy's slug were someterm WordPress would look for taxonomy-sometax-someterm.php. In the case of Post Formats, the taxonomy is 'post_format' and the terms are 'post-format-{format}. i.e. taxonomy-post_format-post-format-link.php

taxonomy-{taxonomy}.php - If the taxonomy were sometax, WordPress would look for taxonomy-sometax.php

taxonomy.php

archive.php

index.php

사용자정의 게시물 유형 표시

archive-{post_type}.php - If the post type were product, WordPress would look for archive-product.php.

archive.php

index.php

(when displaying a single custom post type see the Single Post display section above.)

작성자 표시

author-{nicename}.php - If the author's nice name were rami, WordPress would look for author-rami.php.

author-{id}.php - If the author's ID were 6, WordPress would look for author-6.php.

author.php

archive.php

index.php

날짜 표시

date.php

archive.php

index.php

검색 결과 표시

search.php

index.php

404 (페이지 찾을 수 없음) 표시

404.php

index.php

첨부파일 표시

MIME_type.php - it can be any MIME type (image.php, video.php, application.php). For text/plain, in order:

text.php

plain.php

text_plain.php

attachment.php

single-attachment.php

single.php

index.php

필터 계층 구조

The WordPress templates system allow you to filter the hierarchy. The filter (located in the get_query_template() function) uses this filter name: "{$type}_template" where $type is the a file name in the hierarchy without the .php extension.

Full list:

index_template

404_template

archive_template

author_template

category_template

tag_template

taxonomy_template

date_template

home_template

front_page_template

page_template

paged_template

search_template

single_template

text_template, plain_template, text_plain_template (all mime types)

attachment_template

comments_popup

예

For example, let's take the default author hierarchy:

author-{nicename}.php

author-{id}.php

author.php

To add author-{role}.php before author.php we can manipulate the actual hierarchy using the 'author_template' hook. This allows a request for /author/username where username has the role of editor to display using author-editor.php if present in the current themes directory.