chaihongjun.me

Wordpress模板层级结构简要分析

  了解WordPress 模板层次结构是学习wordpress开发的基础,只有了解了wordpress模板层次结构才可以自由的组织模板,编写自己的wordpress模板主题。

  wordpress的模板层次解析文档:https://developer.wordpress.org/themes/basics/template-hierarchy/

这里还有一张层次图,帮助我们理解模板使用的规则:

template-hierarchy

层级结构简析:

single post

1.single-{post-type}-{slug}.php 

2.single-{post-type}.php

3.single.php

4.singular.php  

5.index.php

说明:

1.slug 也就是当WP的链接为静态时,显示在浏览器地址栏那里的 相当于postname或pagename之类的,比如网址https://chaihongjun.me/cms/ 这里的cms就是slug,前提是开启固定链接且是伪静态。

2.singular.php是4.3新增模板类型,用于自定义呈现单篇内容

single page

1.自定义页面模板  

function get_page_templates( $post = null ) {

    return array_flip( wp_get_theme()->get_page_templates( $post ) );

}

2.page-{slug}.php

3.page-{id}.php

4.page.php

5.singular.php

6.index.php

说明:

  1. id就是此篇文章的id

category分类页面,普通栏目页面 

1.category-{slug}.php

2.category-{id}.php

3.category.php

4.archive.php

5.index.php

说明:

1.archive.php是文档归档页面

Tag

1.tag-{slug}.php 

2.tag-{id}.php

3.tag.php

4.archive.php

5.index.php

Custom Taxonomies 自定义分类

1.taxonomy-{taxonomy}-{term}.php

2.taxonomy-{taxonomy}.php

3.taxonomy.php

4.archive.php

5.index.php

Custom Post Types自定义文章类型

1.archive-{post_type}.php

2.archive.php

3.index.php

Author

1.author-{nicename}.php

2.author-{id}.php

3.author.php

4.archive.php

5.index.php

Date

1.date.php

2.archive.php

3.index.php

Search 搜索结果页

1.search.php

2.index.php

404

1.404.php

2.index.php

附件Attachment

1.MIME_type.php  

text_plain.php

plain.php

text.php

2.attachment.php

3.single-attachment.php

4.single.php

5.index.php

嵌入类型 Embeds

1.embed-{post-type}-{post_format}.php

2.embed-{post-type}.php

3.embed.php

完整的wordpress层次结构可以看这个:http://wphierarchy.com/

知识共享许可协议本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。作者:柴宏俊»