农成学wordpress-第一讲:主题文件构成

"农成学wordpress-第一讲:主题文件构成的图片"

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

文件目录

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

目录结构地址如:wp-content/themes/dmeng2.0/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 主题使用的大小是 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

未经允许不得转载:微信信息发源地 » 农成学wordpress-第一讲:主题文件构成

赞 (7)
分享到:更多 ()

评论 0

评论前必须登录!

登陆 注册