WordPress主题文件基本结构

2013.11.04 2108 人浏览 留言

一般来讲一个主题至少需要包含两个文件:index.php和style.css。

下面是详细介绍WordPress主题的各页面文件:
首页:

  1. home.php
  2. index.php
文章页:

  1. single-{post_type}.php – 如果文章类型是videos(即视频),WordPress就会去查找single-videos.php(WordPress 3.0及以上版本支持)
  2. single.php
  3. index.php

页面:

  1. xxx.php(文件名随便),自定义页面模板 – 在WordPress后台创建页面的时候,右侧边栏可以选择页面的自定义模板
  2. page-{slug}.php – 如果页面的缩略名是news,WordPress将会查找 page-news.php(WordPress 2.9及以上版本支持)
  3. page-{id}.php – 如果页面ID是6,WordPress将会查找page-6.php
  4. page.php
  5. index.php

分类模板:

  1. category-{slug}.php – 如果分类的缩略名为news,WordPress将会查找category-news.php(WordPress 2.9及以上版本支持)
  2. archive-{post_type}.php–如果你的主题有自定义文章类型,比如按照官方网站教程,注册了一个名为book的文章类型,那么它的归档页面模板就是category-book.php,如果没有这个文件,它是不会使用其它文件来代替的,将会直奔404
  3. taxonomy-{taxonomy_slug}.php-这是自定义分类法的分类页,比如上面你注册了一个book的文章类型,然后你注册一个分类法author,它的别名是authors,以便让文章按作者分类。那么这个作者分类页的模板文件就是taxonomy-authors.php
  4. category-{id}.php -如果分类ID为6,WordPress将会查找category-6.php
  5. category.php
  6. archive.php
  7. index.php

标签

  1. tag-{slug}.php – 如果标签缩略名为sometag,WordPress将会查找tag-sometag.php
  2. tag-{id}.php – 如果标签ID为6,WordPress将会查找tag-6.php(WordPress 2.9及以上版本支持)
  3. tag.php
  4. archive.php
  5. index.php

作者:

  1. author-{nicename}.php – 如果作者的昵称为rami,WordPress将会查找author-rami.php(WordPress 3.0及以上版本支持)
  2. author-{id}.php – 如果作者ID为6,WordPress将会查找author-6.php(WordPress 3.0及以上版本支持)
  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 (未找到)页面

  1. 404.php
  2. index.php

附件页面:

  1. MIME_type.php – 可以是任何MIME类型 (image.php, video.php, audio.php, application.php 或者其他).
  2. attachment.php
  3. single.php
  4. index.php

函数

  1. functions.php

评论模板

  1. comments.php

关于上面的文件层次结构
除了上面的自定义文章类型和自定义文章分类需要固定的模板之外,其它的,如果缺少对应文件,可以用其它文件代替。

以WordPress首页为例,下面有2个文件home.php和index.php,WordPress程序会从你的主题文件夹中依次查找这两个文件,如果找到home.php,则使用home.php作为博客首页模板,即使你的主题文件夹中有index.php;如果home.php未找到,则使用index.php作为首页模板;如果home.php和index.php都找不到,你的主题将不会被WordPress识别。

文章地址:https://huilang.me/isay/wordpress-theme-file-basic-structure/

回复本文

您的电子邮箱地址不会被公开。 必填项已用 * 标注

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif