-
WordPress 主题让 WordPress 网站五彩缤纷,是 WordPress 网站的衣服,也是 WordPress 的根本,从模板到功能,WordPress 无所不能,那么,一个完整的 WordPress 主题都由哪些东西构成呢?
文件目录
WordPress 主题位于 WordPress 根目录的 wp-content/themes ,一个 WordPress 主题为一个目录,而这个目录至少需要两个文件,一个是 style.css,一个是 index.php 。
目录结构地址如:wp-content/themes/dexin/style.css
主题目录中的其他文件和目录可以按常见命名习惯命名,如图片目录是 images ,在代码中则使用 get_bloginfo 函数输出主题目录地址,如图片目录地址:get_bloginfo( 'template_directory' ).'/images/';
style.css
style.css 是一个WordPress主题必备的文件,因为需要在这个文件开头使用注释声明主题,如:
/*
Theme Name: Twenty Thirteen
Theme URI: http://wordpress.org/themes/twentythirteen
Author: the WordPress team
Author URI: http://wordpress.org/
Description: The 2013 theme for WordPress takes us back to the blog, featuring a full range of post formats, each displayed beautifully in their own unique way. Design details abound, starting with a vibrant color scheme and matching header images, beautiful typography and icons, and a flexible layout that looks great on any device, big or small.
Version: 1.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: black, brown, orange, tan, white, yellow, light, one-column, two-columns, right-sidebar, flexible-width, custom-header, custom-menu, editor-style, featured-images, microformats, post-formats, rtl-language-support, sticky-post, translation-ready
Text Domain: twentythirteen
This theme, like WordPress, is licensed under the GPL.
Use it to make something cool, have fun, and share what you've learned with others.
*/
以上是来自WordPress自带主题 twentythirteen 的 style.css 文件,含义如下:
Theme Name(主题名称)
Theme URI(主题链接)
Author(主题作者)
Author URI(主题作者链接)
Description(主题简介)
Version(主题版本号)
License(条款)
License URI(条款链接)
Tags(主题标签,用来描述主题的特性,如:响应式)
Text Domain(主题语言域,用于翻译,如 _e( 'Menu', 'twentythirteen' ); 中的 twentythirteen)
index.php
index.php 也是一个必备的文件,它可以代替所有模板文件(假如你没有定义相应的模板的话)。
screenshot.png
screenshot.png 是主题缩略图文件,twentythirteen 主题使用的大小是 880x660px,在后台查看主题时显示,和 style.css 一样位于主题根目录。可有可无,当然有会好看些。
functions.php
functions.php 用来给主题添加功能性代码,比如说替换Gravatar头像稳定服务器地址的代码就是添加在这里 ,一般情况下,定义全局变量,定义函数,使用动作和钩子的代码都会添加在这里。
模板文件
模板文件就太多了,除了这些默认的,你还可以自定义模板。通用头部模板使用 get_header 函数加载,通用底部模板使用 get_footer 函数加载,侧边栏模板使用 get_sidebar 函数加载,其他模板则使用 get_template_part 函数加载。
默认模板的加载顺序会在后面的 WordPress 的模板层次中详细讲解。
通用头部模板:header.php
通用底部模板:footer.php
首页模板:index.php/home.php/front-page.php
文章页模板:single.php
通用归档模板:archive.php
分类归档模板:category.php
标签归档模板:tag.php
日期归档模板:date.php
附件模板:attachment.php
静态页面模板:page.php
作者页模板:author.php
搜索页模板:search.php
搜索框模板:searchform.php
侧边栏模板:sidebar.php
评论框及评论列表模板:comments.php
404模板:404.php
构成代码
在 WordPress 主题中编码你可以和PHP混写(事实上就是写PHP代码,只是预设了大量常用变量和函数让你几乎不用自己写代码,只需要按规则调用即可),需要注意的是,实现一个功能之前最好先查阅一次官方文档确认 WordPress 没有内置再自定义,最大程度精简代码。
同时,WordPress 使用循环加载主要内容,使用模板标签加载指定内容,使用 Action (动作)机制插入处理代码,使用 Filter (钩子)机制过滤数据。
文档直达链接:
内容循环:http://codex.wordpress.org/zh-cn:%E5%BE%AA%E7%8E%AF
模板标签:http://codex.wordpress.org/zh-cn:%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE
条件标签:http://codex.wordpress.org/zh-cn:%E6%9D%A1%E4%BB%B6%E6%A0%87%E7%AD%BE
函数参考:http://codex.wordpress.org/zh-cn:%E5%87%BD%E6%95%B0%E5%8F%82%E8%80%83
动作和钩子:http://codex.wordpress.org/Plugin_API
WordPress主题教程:WordPress主题构成
1443人参与 |分类: 站长百科|时间: 2014年08月20日
相关文章
- 2020-05-09WordPress使用timthumb.php截取文章缩略图
- 2018-11-09一键安装KMS服务脚本 搭建自己的KMS激活服务器教程
- 2018-10-30Windows 10系统的3D Objects(3D对象)文件夹如何删除
- 2018-03-16WINGHO..文件夹删除方法
- 2017-12-23WordPress博客什么时候才需要使用缓存插件?
- 2017-12-05WordPress站点SEO优化中需要避开6个常见错误
- 2017-11-11升级WordPress时提示’另一更新正在进行’的解决方法
- 2017-11-02WordPress上传文件自动重命名
- 2017-10-28WordPress前台显示用户的注册时间
- 2017-08-15WordPress发布/更新文章、提交/审核评论自动清理阿里云CDN缓存
评论专区德信商城