Theme Development

来自站长百科
跳转至: 导航、​ 搜索


The following article is about developing or designing your own WordPress Theme. If you wish to learn more about how to install and use Themes, review the documentation regarding WordPress:Using Themes. This topic differs from WordPress:Using Themes because it discusses the technical aspects of writing code to build your own Themes rather than how to activate Themes or where to obtain new Themes.

下面的文章是关于发展或者设计你自己的WordPress主题。如果你想要学习更多的关于怎样安装和使用主题的知识,请查看关于使用主题的文件。这个话题与使用主题不同,因为主机发展探讨了编写代码来创建你自己的主题的技术方法,而不是激活主题,或者在哪儿能得到新的主题。

You may wish to develop WordPress Themes for your own use or for distribution.

Why WordPress Themes[ ]

你可能因为自己需要使用主题,或者因为发行而发展新主题。

为什么WordPress 主题[ ]

WordPress Themes are files and styles that work together to create a presentation or look for a WordPress site. Each Theme may be different, offering many choices for users to take advantage of in order to instantly change their website look. Why should you build your own WordPress Theme?

WordPress主题是文件和样式的结合体,用来创建WordPress站点的外观或者呈现方式。每个主题可能有所不同,为每个用户提供许多选择,用户利用这些选择来立即地更改它们的站点的外观。你为什么要创建你自己的WordPress主题?

  • To create your own unique WordPress site look
  • 创建你自己的独特的WordPress站点外观
  • To provide alternative templates for specific site features, such as category pages and search result pages.
  • 为特别的站点功能提供可供选择的模板,例如类别网页和搜索结果网页。
  • To quickly switch between two site layouts, or to take advantage of a Theme or style switcher to allow users to change the look of your site.
  • 快速地在两个站点布局之间进行切换,或者利用一个主题或者样式切换器,允许用户改变你的站点的外观。


  • To design WordPress Theme(s) so that others may enjoy your designs through public release.
  • 设计WordPress主题,公开发行,这样其他人享用你的设计。

A WordPress Theme has many benefits, too. WordPress主题也拥有许多的优势。

  • It separates the presentation styles and template files from the system files so the site will upgrade without drastic changes to the visual presentation of the site.
  • 主题将呈现样式和模板文件从系统文件中分开,这样站点更新的时候,不会使站点的外观效果发生巨大的变化。
  • It allows for customization of the presentation and web page results unique to that Theme.
  • WordPress主题允许自定义主题独特的外观和网页内容。
  • It allows for quick changes of the look and feel of a WordPress site.
  • 能够快速地改变一个WordPress站点的外观和整体效果。
  • It takes away the need for a WordPress user to have to learn CSS, HTML, and PHP in order to have a good looking website.


  • 不需要WordPress用户来学习CSS,HTML,和PHP,以创建好的站点外观。


Why should you build your own WordPress Theme? That's the real question.

你为什么应该创建你自己的WordPress主题?这才是真正的问题所在。

  • It's an opportunity to learn more about CSS, HTML/XHTML, and PHP.
  • 这是学习更多的关于CSS,HTML/XHTNL,和PHP知识的一次机会。
  • It's an opportunity to put your expertise with CSS, HTML/XHTML, and PHP to work.
  • 这是一个机会,使你熟练掌握CSS,HTML/XHTML,和PHP。


  • It's creative.
  • 这具有创造性。
  • It's fun (most of the time).
  • 这非常地有趣(大多数时候)。
  • 如果你将它公开发行,你会感觉良好,因为你与WordPress团队一起分享你的主题,并且向WordPress 团队 返回了你的贡献(好的,bragging rights!)

Anatomy of a Theme[ ]

主题的解析[ ]

WordPress Themes live in subdirectories residing in wp-content/themes/. The Theme's subdirectory holds all of the Theme's style sheet files, template files, an optional functions file (functions.php), and images. For example, a Theme named "test" would probably reside in the directory wp-content/themes/test/.

WordPress主题位于wp-content/themes/的子目录中。主题的子目录包含有主题的所有样式表文件,模板文件,一个可选择的的函数文件(functions.php),和图像。例如,一个叫做"测试"的主题可能会在wp-content/themes/test/目录中。

WordPress includes two Themes in the download, a "Classic" and "Default" Theme. The two Themes are different and use different functions and tags to generate their web page results and looks. Examine the files carefully for these Themes to get a better idea of how to build your own Theme files.

WordPress包含有两个主题可以下载,一个是"经典的" 一个是"默认的"主题。这两个主题是不同的并且使用不同的函数和标签来产生各自的网页内容和外观。仔细地检查这些主题的文件来,以知道怎样更好地建立你自己的主题文件。

WordPress Themes consist of three main types of files, in addition to images. One is the style sheet called style.css, which controls the presentation (look) of the web pages. The second is the optional functions file (functions.php). The other files are the template files which control the way the web page generates the information from the Database to be displayed as a web page. Let's look at these individually.

WordPress主题除了图像外,包括三种主要类型的文件。一种是样式表称为style.css,这控制了网页的外表(外观)。第二种是可选择的函数文件(functions.php)。其它的文件是模板文件,这个文件控制了数据库中的信息,在网页上的显示方式。让我们分别地看看这三种文件。


Theme Style Sheet[ ]

主题样式表[ ]

In addition to CSS style information for your theme, the stylesheet, style.css must provide details about the Theme in the form of comments. No two Themes are allowed to have the same details listed in their comment headers, as this will lead to problems in the Theme selection dialog. If you make your own Theme by copying an existing one, make sure you change this information first.

除了提供你的主题的CSS样式信息外,样式表,style.css 必须提供评论形式的主题的详细信息。评论标题上不能列出两个主题同样的详细信息,因为这会引起主题选择对话的错误。如果你通过复制一个现存的主题来制作你自己的主题,首先要确定你已经更改了这个信息。

The following is an example of the first few lines of the stylesheet, called the style sheet header, for the Theme "Rose":

下面是主题的"玫瑰"的样式表前几行的例子,称作样式表标题:

/*   
Theme Name: Rose
Theme URI: the-theme's-homepage
Description: a-brief-description
Author: your-name
Author URI: your-URI
Template: use-this-to-define-a-parent-theme--optional
Version: a-number--optional
.
<pre>/*   
主题名: 玫瑰
主题 URI: 主题的主页
描述: 主要的简述
作者:你的姓名
作者URI: 你的-URI
模板: 使用-这个-来-定义-一个-母-主题—任意的
版本: 一个-数字—随意的。

General comments/License Statement if any.

如果有许可的话,一般的评论/许可声明。

*/
  • /

The simplest Theme includes only a style.css file, plus images, if any. To create such a Theme, you must specify a set of templates to inherit for use with the Theme by editing the Template: line in the style.css header comments. For example, if you wanted the Theme "Rose" to inherit the templates from another Theme called "test", you would include Template: test in the comments at the beginning of Rose's style.css. Now "test" is the parent Theme for "Rose", which still consists only of a style.css file and the concomitant images, all located in the directory wp-content/themes/Rose. (Note that specifying a parent Theme will inherit all of the template files from that Theme — meaning that any template files in the child Theme's directory will be ignored.)

如果存在最简单的主题,这种主题只包括一个style.css文件和图像(如果有图像)。要创建这样的一个主题,你必须通过在style.css标题评论中编辑Template:行,来规定一套模板来继承使用主题。例如,如果你想要"Rose"主题来继承其它的一个称作"测试"的主题的模板,你就会将评论中的Template: test放入Rose的tt>style.css开端。现在"test" 是"Rose"的母主题,只包含有一个style.css文件和一个相关的图像,都在wp-content/themes/Rose目录中。(注规定一个母主题会从主题&mdash中继承所有的主题文件;意思是子主题目录中的任何模板文件都会被忽视。)


The comment header lines in style.css are required for WordPress to be able to identify a Theme and display it in the Administration Panel under Design > Themes as an available Theme option along with any other installed Themes.

style.css中的评论标题行要求WordPress能够辨别一个主题并且将主题显示在设计 > 主题下的管理面板与其它安装好的主题一起,作为可选择的主题。

Note : When defining the parent Theme, in the Template: section of the comment header, you must use the name of the directory of the style. For example, to use as parent template the Default Wordpress Theme, don't write Template: WordPress Default, but Template: default, because default is the directory of this Theme.

 : 当在评论标题的Template:部分定义一个母主题的时候,你必须使用那个样式的目录名。例如,将默认的WordPress主题用作母主题,不要写成模板: WordPress 默认,要写成模板: 默认,因为默认是这个主题的目录。


Functions File[ ]

函数 文件[ ]

A theme can optionally use a functions file, which resides in the theme subdirectory and is named functions.php. This file basically acts like a plugin, and if it is present in the theme you are using, it is automatically loaded during WordPress initialization (both for admin pages and external pages). Suggested uses for this file:

主题可以随意地使用函数文件,这个文件在主题子目录中,并且命名为functions.php。文件基本像一个插件那样运行,而且如果文件呈现在你使用的主题中,会在WordPress初始化(管理网页和外部网页)的时候,自动载入。建议使用这个文件:

  • Define functions used in several template files of your theme
  • Set up an admin screen, giving users options for colors, styles, and other aspects of your theme
  • 定义你的主题多个模板文件中使用的函数
  • 设置一个管理界面,提供给用户颜色,样式,和你的主题的其它方面的选项

The "Default" WordPress theme contains a functions.php file that defines functions and an admin screen, so you might want to use it as a model. Since functions.php basically functions as a plugin, the WordPress:Function_Reference list is the best place to go for more information on what you can do with this file.

"默认"WordPress主题包含一个functions.php文件,这个文件定义了函数和一个管理界面,这样你可能想将这个文件用作一个模型。因为functions.php基本起一个插件的作用,函数_参考列表有你怎样处理这个文件的更多的信息。

Theme Template Files[ ]

主题模板文件[ ]

Templates are PHP source files used to generate the pages requested by visitors. Let's look at the various templates that can be defined as part of a Theme.

模板是PHP原始文件,根据访客要求,用来产生网页的。让我们来看看可以被定义为主题部分的不同的模板。

WordPress allows you to define separate templates for the various aspects of your weblog; however, it is not essential to have all these different template files for your blog to function fully. Templates are chosen and generated based upon the WordPress:Template Hierarchy, depending upon what templates are available in a particular Theme. As a Theme developer, you can choose the amount of customization you want to implement using templates. For example, as an extreme case, you can use only one template file, called index.php as the template for all pages generated and displayed by the weblog. A more common use is to have different template files generate different results, to allow maximum customization.

WordPress能够使你为你的博客的不同部分分别定义模板;然而,使你的博客正常地运行,并不一定需要所有的这些不同的模板文件。模板是根据模板层级选择和产生的,取决于特别的主题中拥有什么样的模板。作为一个主题开发者,你可以选择你想使用模板,自定义多少内容。例如,一个极端的例子,你可以为网络日志上产生的和显示的所有的网页,只使用一个称为index.php模板文件,作为模板。一个更加通常的用法是使用不同的模板文件产生不同的结果,允许最大化的自定义。

Basic Templates[ ]

基本的模板[ ]

At the very minimum, a WordPress Theme consists of two files:

在最小值的时候, WordPress主题包含有两个文件:

  • style.css
  • index.php
  • style.css
  • index.php

Both of these files go into the Theme's directory. The index.php template file is very flexible. It can be used to include all references to the header, sidebar, footer, content, categories, archives, search, error, and other web pages generated by the user on your site. Or it can be subdivided into modular template files, each one taking on part of the workload.

这两个文件都归属于主题的目录。index.php 模板文件非常地灵活。它可以被用来包括关于标题,工具条,页脚,内容,类别,归档,搜索,错误和你的站点上的用户创建的其它的网页的所有参数。或者它可以被再次地分到模模板文件,每个部分都具有一部分的工作量。

If you do not provide any other template files, WordPress will use the built-in default files. For example, if you do not have either a comments.php or comments-popup.php template file, then WordPress will automatically use the wp-comments.php and wp-comments-popup.php template files using WordPress:Template Hierarchy. These default templates may not match your Theme very well, so you probably will want to provide your own. The basic files normally used to subdivide (which go into the Theme's directory) are:

如果你没有提供其它任何的模板文件,WordPress会使用内置的默认文件。例如,如果你既没有comments.php也没有comments-popup.php模板文件,WordPress就会使用模板层级,自动地使用wp-comments.phpwp-comments-popup.php模板文件。这些默认模板可能不太匹配你的主题,因此你可能需要提供你自己的模板。用来再次地切分的基本的文件(归属于主题的目录)是:

  • header.php
  • sidebar.php
  • footer.php
  • comments.php
  • comments-popup.php
  • header.php
  • sidebar.php
  • footer.php
  • comments.php
  • comments-popup.php

Using these modular template files, you can put template tags within the index.php master file to include or get these units where you want them to appear in the final generated web page.

使用这些模的模板文件,你可以将这些模板标签放到index.php主要文件中来包括或者得到这些units,你想让它们出现在最后产生的网页的位置。


Here is an example of the include usage:

下面是关于包含 用法的一个例子:

<?php get_sidebar(); ?>

<?php get_footer(); ?>


<?php get_sidebar(); ?>

<?php get_footer(); ?>

For more on how these various Templates work and how to generate different information within them, read the WordPress:Templates documentation.

关于这些不同模板怎样运行以及怎样在模板里面产生不同信息的更多内容,请阅读模板文件。

Query-based Templates[ ]

以查询为基础的模板[ ]

WordPress can load different Templates for different query types. There are two ways to do this: as part of the built-in WordPress:Template Hierarchy, and through the use of WordPress:Conditional Tags within WordPress:The Loop of a template file.

WordPress可以为不同的查询 形式载入不同的模板。有两种方法可以做到这一点:作为内置的模板层级的一部分,通过在模板文件的Loop中使用条件式标签

To use the WordPress:Template Hierarchy, you basically need to provide special-purpose Template files, which will automatically be used to override index.php. For instance, if your Theme provides a template called category.php and a category is being queried, category.php will be loaded instead of index.php. If category.php is not present, index.php is used as usual.

要使用模板层级,你只要提供特殊用途的模板文件,这个文件会自动地覆盖index.php。例如,如果你的主题提供一个叫做category.php的模板,并且提供一个正被查询的类别,category.php而不是index.php将会被载入。如果category.php没有出现,index.php会照常使用。

You can get even more specific in the Template Hierarchy by providing a file called, for instance, category-6.php -- this file will be used rather than category.php when generating the page for the category whose ID number is 6. (You can find category ID numbers in Manage > Categories if you are logged in as the site administrator in WordPress version 2.3 and below. In WordPress 2.5 the ID column was removed from the Admin panels. You can locate the category id by clicking 'Edit Category' and looking on the URL address bar for the cat_ID value. It will look '...categories.php?action=edit&cat_ID=3' where '3' is the category id). For a more detailed look at how this process works, see WordPress:Category Templates.

通过提供一个例如,称作category-6.php的文件,你可以在模板层级中得到更加详细的信息---当为一个ID号码为6的类别产出网页的时候,这个文件而不是category.php会被用到。(如果你在WordPress2.3版本以及更低的版本中,以站点管理员的身份登录,你在 管理 > 类别 下面会看到这个类别的ID数字。在WordPress 2.5版本中,管理面板中移除了ID栏。通过点击'编辑类别' ,并且查找URL地址中的cat_ID 参数值,你可以查找类别id的位置。看起来就像'...categories.php?action=edit&cat_ID=3' 其中'3'就是类别 id)。更详细地看看这个过程是怎样进行的,请看看类别模板

If your Theme needs to have even more control over which Template files are used than what is provided in the WordPress:Template Hierarchy, you can use WordPress:Conditional Tags. The Conditional Tag basically checks to see if some particular condition is true, within the WordPress Loop, and then you can load a particular template, or put some particular text on the screen, based on that condition.

如果你的主题需要更多地控制使用的哪个模板文件而不是模板层级中所提供的内容,你可以使用条件式标签。条件式标签基本上是用来检查看看在WordPress Loop内的,一些特别的条件是否是真的,然后你就可以根据那个情况,载入一个特别的模板,或者在屏面上放一些特别的文本。

For example, to generate a distinctive style sheet in a post only found within a specific category, the code might look like this:

例如,给一个特别类别中的文章创建一个样式表,代码可能看起来像这样的:

<?php
if (is_category(9)) {
   // looking for category 9 posts
   include(TEMPLATEPATH . '/single2.php');
} else {
   // put this on every other category post
   include(TEMPLATEPATH . '/single1.php');
}
?>
<?php
if (is_category(9)) {
   //查找类别9中的文章
  包括(TEMPLATEPATH . '/single2.php');
} else {
   // 每隔一篇文章便放入这个
   include(TEMPLATEPATH . '/single1.php');
}
?>

Or, using a query, it might look like this:

或者,使用一个查询,这个查询可能看起来像:

<?php
$post = $wp_query->post;
if ( in_category('9') ) {
   include(TEMPLATEPATH . '/single2.php');
} else {
   include(TEMPLATEPATH . '/single1.php');
}
?>
<?php
$post = $wp_query->post;
if ( in_category('9') ) {
   include(TEMPLATEPATH . '/single2.php');
} else {
   include(TEMPLATEPATH . '/single1.php');
}
?>

In either case, this example code will cause different templates to be used depending on the category of the particular post being displayed. Query conditions are not limited to categories, however -- see the WordPress:Conditional Tags article to look at all the options.

在上述任一例子中,代码会产生不同的模板,这些模板的使用取决于类别上显示的文章。查询条件并不局限于类别,然而—请看看条件式标签文章来看看所有的选项。


Media Icons[ ]

媒体图标[ ]

This feature is currently broken in WordPress 2.5.

这个功能当前在 WordPress 2.5中破坏了

Wordpress uses media icons to represent attachment files on your blog and in the Admin interface, if those icons are available.

WordPress使用媒体icons来代表你的博客和管理界面上的附属文件,前提是存在这些图标。

It looks for image files named by media type in the images directory of the current theme. (As of Wordpress 2.2, the default theme comes with only one media icon, audio.jpg.)

它要查找当前主题的images目录中的媒体类型所命名的图像文件。(从Wordpress2.2开始,默认的主题只有一个媒体图标,audio.jpg。)


For example, for an attachment of MIME type audio/mpeg, Wordpress would look for an icon file at these locations, stopping after the first match (see wp_mime_type_icon):

例如,对于MIME 类型 audio/mpeg的一个附属装置,WordPress在这些位置中寻找一个图标文件,在找到第一个匹配文件后,便停止(请看看wp_mime_type_icon):

  1. my_theme/images/audio.jpg
  2. my_theme/images/audio.gif
  3. my_theme/images/audio.png
  4. my_theme/images/mpeg.jpg
  5. my_theme/images/mpeg.gif
  6. my_theme/images/mpeg.png
  7. my_theme/images/audio_mpeg.jpg
  8. my_theme/images/audio_mpeg.gif
  9. my_theme/images/audio_mpeg.png


  1. my_theme/images/audio.jpg
  2. my_theme/images/audio.gif
  3. my_theme/images/audio.png
  4. my_theme/images/mpeg.jpg
  5. my_theme/images/mpeg.gif
  6. my_theme/images/mpeg.png
  7. my_theme/images/audio_mpeg.jpg
  8. my_theme/images/audio_mpeg.gif
  9. my_theme/images/audio_mpeg.png

Theme Template Files List[ ]

主题模板文件列表[ ]

Here is the list of Theme template files recognized by WordPress. Of course, your Theme can contain any other style sheets, images, or files. Just keep in mind that the following have special meaning to WordPress -- see WordPress:Template Hierarchy for more information.

下面是WordPress 识别的主题模板文件的列表。当然,你的主题可以包含任何其它的样式表,图像,或者文件。只要牢记下面的内容对于WordPress具有一些特别的意义—更多的信息请看看模板层级

style.css
The main stylesheet. This must be included with your Theme, and it must contain the information header for your Theme.
style.css
主要的样式表。这个必须包含在你的主题中,而且必须包含你的主题的信息标题。
index.php
The main template. If your Theme provides its own templates, index.php must be present.
index.php
主要的模板。如果你的主题提供自己的模板,index.php必须呈现。
comments.php
The comments template. If not present, comments.php from the "default" Theme is used.
comments.php
评论模板。如果没有出现,来自"默认"主题的comments.php就会被使用。
comments-popup.php
The popup comments template. If not present, comments-popup.php from the "default" Theme is used.
comments-popup.php
弹出的评论模板。如果没有出现,来自"默认"主题的comments-popup.php就被使用。


home.php
The home page template.
home.php
主页模板。
single.php
The single post template. Used when a single post is queried. For this and all other query templates, index.php is used if the query template is not present.
single.php
单一的文章模板。当一篇单一的文章被查询的时候,便会用到这个模板。对于这个和其它的查询模板,如果查询模板没有出现,就会用到index.php
page.php
The page template. Used when an individual Page is queried.
page.php
网页模板。当一个单独的网页得到查询的时候,便会用到这个。
category.php
The category template. Used when a category is queried.
category.php
The 类别模板。当询问类别的时候,便会用到这个。
author.php
The author template. Used when an author is queried.
author.php
作者模板。查询作者的时候,会使用到这个。
date.php
The date/time template. Used when a date or time is queried. Year, month, day, hour, minute, second.
date.php
日期/时间模板。当查询日期或者时间的时候,便会用到这个模板。年,月,日,时,分,秒。
archive.php
The archive template. Used when a category, author, or date is queried. Note that this template will be overridden by category.php, author.php, and date.php for their respective query types.
archive.php
归档模板。当查询一个类别,作者或者日期的时候,便会用到这个模板。注意这个模板会被category.php, author.php, 和date.php各自的查询形式覆盖。
search.php
The search results template. Used when a search is performed.
404.php
The 404 Not Found template. Used when WordPress cannot find a post or page that matches the query.
search.php
搜索结果模板。当执行一个搜索的时候,便会用到这个模板。
404.php
404 没有找到模板。当WordPress找不到匹配查询的文章或者网页的时候,便会用到这个模板。

These files have a special meaning with regard to WordPress because they are used as a replacement for index.php, when available, according to the WordPress:Template Hierarchy, and when the corresponding Conditional Tag (a.k.a is_*(); function) returns true. For example, if only a single post is being displayed, the is_single() function returns 'true', and, if there is a single.php file in the active Theme, that template is used to generate the page.

这些文件有关于WordPress的特别意思因为这些文件用来作为index.php替代品,条件是这些文件存在,根据模板层级,而且相应的条件式标签 (a.k.a is_*(); 函数) 返回是真。例如,如果只显示一篇文章,is_single()函数返回'真的',如果归档主题中有single.php文件,这个模板就用来创建网页。

Referencing Files From a Template[ ]

参考模板中的文件[ ]

The WordPress Default Theme (based on Michael Heilemann's Kubrick layout for WordPress 1.2) provides a good example of how queries are mapped onto templates.

WordPress默认主题(以Michael Heilemann的KubrickWordPress1.2布局为基础)提供了一个非常好的例子,关于查询是怎样映射进模板的。

The code <?php bloginfo('template_directory'); ?> inserts the URL of the template directory into the template output. You can append any additional URI information to this output to reference files in your Theme.

代码<?php bloginfo('template_directory'); ?>将模板目录的URL嵌入了模板输出中。你可以将任何额外的URI信息添加到你的主题的参考文件的这个输出中。

The code <?php bloginfo('stylesheet_directory'); ?> inserts the URL of the directory that contains the current Theme stylesheet into the template output. You can append any additional URI information to this output to reference files for your Theme, specifically those that are used by the stylesheet.

代码<?php bloginfo('stylesheet_directory'); ?>嵌入了目录的URI,这个URI将当前的主题样式表包含到模板输出中。你可以添加任何额外的URI到你的主题的参考文件的输出中,特别是样式表使用的那些。

The constant TEMPLATEPATH is a reference to the absolute path to the template directory for the current Theme (without the / at the end).

恒量TEMPLATEPATH是当前主题模板目录绝对路径的一个参考(后面没有/)。

Note that URIs that are used in the stylesheet are relative to the stylesheet, not the page that references the stylesheet. This obviates the need to include PHP code in the CSS file to specify directories. For example, if you include an images/ directory in your Theme, you need only specify this relative directory in the CSS, like so:

注意用在样式表中的URIs与样式表相关,但是与指代样式表的网页并不相关。这就避免了在CSS文件中包含PHP代码来规定类别。例如,如果你的主题包含了一个images/目录,你只需要在CSS中规定这个相关的目录,像这样的:

h1 { background-image: URL(images/my_background.jpg); }
h1 { background-image: URL(images/my_background.jpg); }

It is a good practice to use URIs in the manner described above to reference files from within a template, since, then your template will not depend on absolute paths.

对于模板中的参考文件以以上的方式使用URIs是一个好的做法,因为,这样你的模板就不会依赖于绝对路径了。

Defining Custom Templates[ ]

定义自定义模板[ ]

It is possible to use the WordPress plugin system to define additional templates that are shown based on your own custom criteria. This advanced feature can be accomplished using the template_redirect action hook. More information about creating plugins can be found in the WordPress:Plugin API reference.

可以使用WordPress插件系统来定义额外的模板,这种模板以你自己自定义的标准,显示。这个高级的功能通过使用template_redirect action hook可以得到实现。更多的关于创建插件的信息可以在插件API参考中找到。

Plugin API Hooks[ ]

插件API Hooks[ ]

When developing Themes, it's good to keep in mind that your Theme should be set up so that it can work well with any WordPress plugins you (or another Theme user) might decide to install. Plugins add functionality to WordPress via "Action Hooks" (see WordPress:Plugin API for more information). Most Action Hooks are within the core PHP code of WordPress, so your Theme does not have to have any special tags for them to work. But a few Action Hooks do need to be present in your Theme, in order for Plugins to display information directly in your header, footer, sidebar, or in the page body. Here is a list of the special Action Hook Template Tags you need to include:

发展主题的时候,最好牢记你的主题得到了设置,这样它就能够很好地配合你(或者其它的主题用户)可能决定安装的WordPress插件。插件通过"Action Hooks"来给WordPress添加功能(请看看插件API得到更多的信息)。大多数Action Hooks是在WordPress核心PHP代码内部,因此你的主题不要为它们的运行准备任何特别标签。但是有几个Action Hooks的确需要呈现在你的主题上,为了使插件能够直接地在你的页眉,页脚,工具条,或者在网页上显示信息。下面是你需要的特别Action Hook模板标签列表:

wp_head
wp_head
Goes in the HTML <head> element of a theme; header.php template. Example plugin use: add javascript code.
进入 HTML <head> 主题元素; header.php 模板。例子插件使用: 添加 javascript 代码。
Usage: <?php do_action('wp_head'); ?>
-or-  <?php wp_head(); ?>
用法: <?php do_action('wp_head'); ?>
-or-  <?php wp_head(); ?>
wp_footer
wp_footer
Goes in the "footer" of a theme; footer.php template. Example plugin use: insert PHP code that needs to run after everything else, at the bottom of the footer.
进入主题的"页脚"中; footer.php 模板。例子插件使用: 在页脚的底部,嵌入需要在所有内容运行之后运行的PHP代码。
Usage: <?php do_action('wp_footer'); ?>
-or-  <?php wp_footer(); ?>
用法: <?php do_action('wp_footer'); ?>
-or-  <?php wp_footer(); ?>
wp_meta
wp_meta
Typically goes in the <li>Meta</li> section of a theme's menu or sidebar; sidebar.php template. Example plugin use: include a rotating advertisement or a tag cloud.
通常进入主题的菜单或者工具条的<li>Meta</li>部分; sidebar.php 模板。 例子插件使用: 包括旋转的广告或者标签cloud。
Usage: <?php do_action('wp_meta'); ?>
-or-  <?php wp_meta(); ?>
用法: <?php do_action('wp_meta'); ?>
-or-  <?php wp_meta(); ?>
comment_form
comment_form
Goes in comments.php and comments-popup.php, directly before the comment form's closing tag (</form>). Example plugin use: display a comment preview.
进入 comments.phpcomments-popup.php中, 直接在评论形式的关闭标签 (</form>)前面。 例子插件使用: 显示一个评论预览。
Usage: <?php do_action('comment_form', $post->ID); ?>
用法: <?php do_action('comment_form', $post->ID); ?>

For a real world usage example, you'll find these plugin hooks included in the default theme's templates.

对于真实的世界用法的例子,你将会发现这些插件hooks包含在默认主题的模板中。

Theme Development General Guidelines[ ]

主题发展一般指导方针[ ]

Please be clear about the following in your documentation (a README file included with your Theme helps many users over any potential stumbling blocks):

请了解你的文件中的以下内容(一个包含在你的主题中的README文件帮助许多用户克服了许多潜在的问题):

  1. Indicate precisely what your Theme and template files will achieve.
  2. 明确地指出你的主题和模板文件要获得什么。
  3. Indicate deficiencies in your Themes, if any.
  4. 如果你的主题有什么缺陷的话,将它指出来。
  5. Clearly reference any special modifications in comments within the template and style sheet files. Add comments to modifications, template sections, and CSS styles, especially those which cross template files.
  1. 明确地指出模板和样式表文件中评论任何特别的修改。给更改,模板部分,CSS样式,特别是那些穿过模板文件的CSS样式添加评论。
  2. If you have any special requirements, which may include custom RewriteRules, or the use of some additional, special templates, images or files, please explicitly state the steps of action a user should take to get your Theme working.
  1. 如果你有什么特别的要求,这些要求可能包括自定义RewriteRules,或者使用一些额外的,特别的模板,图像或者文件,请清楚地描述用户应该采取那些措施,以使你的主题运行。
  1. Try and test your Theme across browsers to catch at least a few of the problems the users of the Theme may find later.
  1. 试着测试你的主题跨越浏览器至少发现几个,未来主题用户可能碰到的问题
  2. Provide contact information (web page or email), if possible, for support information and questions.
  1. 如果可能的话,提供联系信息(网页或者电子邮件),来支持信息和问题。

Take time to read through WordPress:Designing Themes for Public Release, an article with good tips on preparing your Theme for the public.

花一些时间来通读为了公共发行而设计主题,这篇文章有许多关于怎样准备你的主题来公共发行的好的贴士。

References and Resources[ ]

参考和资源[ ]

There is a comprehensive list of WordPress Theme and Template File resources in the WordPress:Templates article.

模板文章中有WordPress主题和模板文件资源。