×

WordPress主题制作教程(二):主题文件结构

优畅主题 Wordpress技术 2014-12-22 22:03 4,891 0 条评论

WordPress主题让WordPress网站五彩缤纷,是WordPress网站的衣服,也是WordPress的根本,从模板到功能,WordPress无所不能,那么,一个完整的WordPress主题都由哪些东西构成呢?

文件目录

WordPress主题位于WordPress根目录的 wp-content/themes ,一个WordPress主题为一个目录,而这个目录至少需要两个文件,一个是 style.css,一个是 index.php 。

目录结构地址如:wp-content/themes/uctheme/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 文件,含义如下:

index.php

index.php 也是一个必备的文件,它可以代替所有模板文件(假如你没有定义相应的模板的话)。

screenshot.png

screenshot.png 是主题缩略图文件,twentythirteen 主题使用的大小是 880×660px,在后台查看主题时显示,和 style.css 一样位于主题根目录。可有可无,当然有会好看些。

functions.php

functions.php 用来给主题添加功能性代码,比如说替换Gravatar头像稳定服务器地址的代码就是添加在这里 ,一般情况下,定义全局变量,定义函数,使用动作和钩子的代码都会添加在这里。

模板文件

模板文件就太多了,除了这些默认的,你还可以自定义模板。通用头部模板使用 get_header 函数加载,通用底部模板使用 get_footer 函数加载,侧边栏模板使用 get_sidebar 函数加载,其他模板则使用 get_template_part 函数加载。

默认模板的加载顺序会在后面的WordPress的模板层次中详细讲解。

  1. 通用头部模板:header.php
  2. 通用底部模板:footer.php
  3. 首页模板:index.php/home.php/front-page.php
  4. 文章页模板:single.php
  5. 通用归档模板:archive.php
  6. 分类归档模板:category.php
  7. 标签归档模板:tag.php
  8. 日期归档模板:date.php
  9. 附件模板:attachment.php
  10. 静态页面模板:page.php
  11. 作者页模板:author.php
  12. 搜索页模板:search.php
  13. 搜索框模板:searchform.php
  14. 侧边栏模板:sidebar.php
  15. 评论框及评论列表模板:comments.php
  16. 404模板:404.php

构成代码

在WordPress主题中编码你可以和PHP混写(事实上就是写PHP代码,只是预设了大量常用变量和函数让你几乎不用自己写代码,只需要按规则调用即可),需要注意的是,实现一个功能之前最好先查阅一次官方文档确认WordPress没有内置再自定义,最大程度精简代码。

同时,WordPress使用循环加载主要内容,使用模板标签加载指定内容,使用 Action (动作)机制插入处理代码,使用 Filter (钩子)机制过滤数据。

文档直达链接:

  1. 内容循环:http://codex.wordpress.org/zh-cn:%E5%BE%AA%E7%8E%AF
  2. 模板标签:http://codex.wordpress.org/zh-cn:%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE
  3. 条件标签:http://codex.wordpress.org/zh-cn:%E6%9D%A1%E4%BB%B6%E6%A0%87%E7%AD%BE
  4. 函数参考:http://codex.wordpress.org/zh-cn:%E5%87%BD%E6%95%B0%E5%8F%82%E8%80%83
  5. 动作和钩子:http://codex.wordpress.org/Plugin_API

如果本文对你有帮助,你可以扫描右边的二维码打赏,谢谢支持
联系优畅:uctheme#qq.com (#改为@)
微信订阅号:优畅主题(uctheme)
官网淘宝店:http://uctheme.taobao.com
版权声明:版权归 优畅主题 所有,转载请注明出处!
转载请保留链接: https://www.uctheme.com/technical/1143.html
谢谢支付宝打赏
谢谢微信打赏

品牌创立:2012-11-18优畅主题

优畅主题成立于2012年11月18日,专业Wordpress导购主题开发商,拥有多年Wordpress主题设计经验,专门为淘宝客和导购客站长量身打造高端赚钱模板,我们注重细节,有着严谨的开发态度,一切从客户角度出发,如果你也喜欢 WordPress导购主题,欢迎和我们一起交流!


5 + 5 = ?

目前还没有评。

切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册