打开wp-config.php,设置调制模式为开启,开发结束后记得要回来关闭哦;
define('WP_DEBUG',true);
主题目录结构
- wp-content
- themes
- yourtheme
这里博主命名了一个中二的名字'tsubasa';
- wp-content
- themes
- tsubasa
- index.php
- style.css
在wordpress中,主题相关信息存储在style.css
中,比如以下是Twenty Sixteen
主题的一个头部说明片断;
/*
Theme Name: Twenty Sixteen
Theme URI: https://wordpress.org/themes/twentysixteen/
Author: the WordPress team
Author URI: https://wordpress.org/
Description: Twenty Sixteen is a modernized take on an ever-popular WordPress layout — the horizontal masthead with an optional right sidebar that works perfectly for blogs and websites. It has custom color options with beautiful default color schemes, a harmonious fluid grid using a mobile-first approach, and impeccable polish in every detail. Twenty Sixteen will make your WordPress look beautiful everywhere.
Version: 1.3
......
*/
学习过程中我们暂时不用写得这么复杂,可以先输入一些基础信息比如:
/*
Theme Name: tsubasa
Author: alphonse
Description: just so so
Version: 1.0
*/
在index.php中放入一个标准模板:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>主题首页</title>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
这两个文件构成了一个最小主题,你甚至可以直接将其打包上传至服务器试运行,当然这个极简主题除了现实一行“Hello World!”并不具备什么意义。
主题常见文件
//常见文件
style.css:主题样式文件;
index.php:主题入口文件;
header.php:页眉内容;
footer.php:页脚内容;
single.php:单篇文章模板;
page.php:分页页面模板;
functions.php:主题功能文件,存放一些主题支持函数,添加自定义功能;
//常见文件夹
assets:存放css,js和图像等资源的文件夹;
inc:存放php文件,文件内一般是自定义函数;
太长不看的加长版本
css(文件夹):存放 css 文件
genericons(文件夹):存放字体文件
inc(文件夹):存放 php 文件,文件内一般是自定义函数
js(文件夹):存放 js 文件
languages(文件夹):存放语言文件
template-parts(文件夹):为了代码简洁,作者把一些代码(可能重复用到的)分开放到了此文件夹下
404.php:出现404错误时使用的文件
archive.php:分类页面
comments.php:评论
footer.php:页脚
functions.php:定义函数
header.php:页头
image.php:图片
index.php:首页
page.php:页面(page)
rtl.css:关于字体的一些样式
screenshot.php:主题缩略图,在后台显示
search.php:搜索页面
searchform.php:搜索框
sidebar.php:侧边栏
sidebar-content-bottom.php:侧边栏底部
single.php:文章(post)页面
style.css:样式文件
更可以跳过的文件执行优先级
以主页为例,下面有2个文件 home.php 和 index.php,WordPress 程序会从你的主题文件夹中依次查找这两个文件(后台设置首页显示为”最新文章”的前提下):
如果找到 home.php,则使用 home.php 作为博客首页模板,即使你的主题文件夹中有 index.php;
如果 home.php 未找到,则使用 index.php 作为首页模板;
如果 home.php 和 index.php 都找不到,你的主题将不会被 WordPress 识别,等于白做。
主页
home.php
index.php
文章页:
single-{post_type}.php – 如果文章类型是videos(即视频),WordPress就会去查找single-videos.php(WordPress 3.0及以上版本支持)
single.php
index.php
页面
自定义模板 – 在WordPress后台创建页面的地方,右侧边栏可以选择页面的自定义模板
page-{slug}.php – 如果页面的缩略名是news,WordPress将会查找 page-news.php(WordPress 2.9及以上版本支持)
page-{id}.php – 如果页面ID是6,WordPress将会查找page-6.php
page.php
index.php
分类
category-{slug}.php – 如果分类的缩略名为news,WordPress将会查找category-news.php(WordPress 2.9及以上版本支持)
category-{id}.php -如果分类ID为6,WordPress将会查找category-6.php
category.php
archive.php
index.php
标签
tag-{slug}.php – 如果标签缩略名为sometag,WordPress将会查找tag-sometag.php
tag-{id}.php – 如果标签ID为6,WordPress将会查找tag-6.php(WordPress 2.9及以上版本支持)
tag.php
archive.php
index.php
作者
author-{nicename}.php – 如果作者的昵称为rami,WordPress将会查找author-rami.php(WordPress 3.0及以上版本支持)
author-{id}.php – 如果作者ID为6,WordPress将会查找author-6.php(WordPress 3.0及以上版本支持)
author.php
archive.php
index.php
日期页面
date.php
archive.php
index.php
搜索结果
search.php
index.php
404 (未找到)页面
404.php
index.php
附件页面
MIME_type.php – 可以是任何MIME类型 (image.php, video.php, audio.php, application.php 或者其他).
attachment.php
single.php
index.php
可以看到,最低优先级都是
index.php
,也就是说,没有找到优先级靠前的专属文件时,系统最后都可以拿index.php
做为备胎,这也是前文2个文件组成一个最low主题的理论基础。