wordpress主题是由一系列模板文件组成,文件名已经约定俗成,有固定的名称或规则。wordpress的主题是存放在/wp-content/themes目录下的一个个文件夹,一个文件夹对应一个主题,文件夹下存放若干模板文件。参考下图:
接下来我们就仔细看下这些文件和他们的作用,以传统主题为例(classic theme)
index.php
这个是主文件,所有模板都需要,这里也顺便提下wordpress开发语言是php
style.css
这个是主要的样式文件,同时它还包含了主题的重要信息说明,比如作者,版本,主题名称等等,看下官方的一个实例
/*
Theme Name: Twenty Twenty-One
Theme URI: https://wordpress.org/themes/twentytwentyone/
Author: the WordPress team
Author URI: https://wordpress.org/
Description: Twenty Twenty-One is a blank canvas for your ideas and it makes the block editor your best brush. With new block patterns, which allow you to create a beautiful layout in a matter of seconds, this theme’s soft colors and eye-catching — yet timeless — design will let your work shine. Take it for a spin! See how Twenty Twenty-One elevates your portfolio, business website, or personal blog.
Requires at least: 5.3
Tested up to: 5.8
Requires PHP: 5.6
Version: 1.4
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: twentytwentyone
Tags: one-column, accessibility-ready, custom-colors, custom-menu, custom-logo, editor-style, featured-images, footer-widgets, block-patterns, rtl-language-support, sticky-post, threaded-comments, translation-ready
Twenty Twenty-One WordPress Theme, (C) 2020 WordPress.org
Twenty Twenty-One is distributed under the terms of the GNU GPL.
*/
style-rtl.css
如果网站的文字方向是从右到左的,那就会加载这个样式文件。通常情况下,我们文本的都是从左到右的。如果需要,我们可以使用RTLCSS或者CSSJanus来创建rtl版的style.css,命名为style-rtl.css即可。
front-page.php
如果存在这个文件,就被设置成首页。
home.php
默认这个是首页,如果没有设置静态的首页,那这个就用来显示最新的文章。
singular.php
如果single.php不存在,或者page.php不存在,那就会用singular.php来显示文章,如果这个文件也不存在,就使用index.php
single.php (classic theme) or single.html (block theme)
用户访问某一篇文章时,会使用single.php页面。
single-{post-type}.php
对于特定类型的文章比如book类型,用户访问时,会使用single-book.php页面。
archive-{post-type}.php
用户访问特定类型的归档文章时,会使用这个文件,如果不存在,则使用archive.php。
page.php
用户访问页面时,会使用这个文件
page-{slug}.php
用户访问特定的某一个页面时,会使用这个文件,比如page-about.php
category.php
用户通过类别来访问文章时,会使用这个页面
tag.php
用户通过标签(Tag)来访问文章时,会使用这个页面
taxonomy.php
通常情况wordpress是通过类别、标签来组织文章,但使用taxonomy,可以自定义组织和排序文章内容,taxonomy还可以有下级taxonomy
author.php
用户通过作者来访问文章时,会使用这个页面
date.php
用户通过日期来访问文章时,会使用这个页面,举例:
http://example.com/blog/2014/
http://example.com/blog/2014/05/
http://example.com/blog/2014/05/26/
archive.php
用户通过类别,作者或者日期来访问文章时,会使用这个页面 The archive template is used when visitors request posts by category, author, or date. Note: this template will be overridden if more specific templates are present like category.php, author.php, and date.php.
search.php
用来显示搜索
attachment.php
用来显示单个附件,比如图片,pdf等.
image.php
显示图片类型附件,如果没有,会调用attachment.php
404.php
如果wordpress没找到页面,就会返回404
comments.php
用来显示评论