站长百科 | 数字化技能提升教程 数字化时代生存宝典
首页
数字化百科
电子书
建站程序
开发
服务器
办公软件
开发教程
服务器教程
软件使用教程
运营教程
热门电子书
WordPress教程
宝塔面板教程
CSS教程
Shopify教程
导航
程序频道
推广频道
网赚频道
人物频道
网站程序
网页制作
云计算
服务器
CMS
论坛
网店
虚拟主机
cPanel
网址导航
WIKI使用导航
WIKI首页
最新资讯
网站程序
站长人物
页面分类
使用帮助
编辑测试
创建条目
网站地图
站长百科导航
站长百科
主机侦探
IDCtalk云说
跨境电商导航
WordPress啦
站长专题
网站推广
网站程序
网站赚钱
虚拟主机
cPanel
网址导航专题
云计算
微博营销
虚拟主机管理系统
开放平台
WIKI程序与应用
美国十大主机
编辑“
Theme Development
”
人物百科
|
营销百科
|
网赚百科
|
站长工具
|
网站程序
|
域名主机
|
互联网公司
|
分类索引
跳转至:
导航
、
搜索
警告:
您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您
登录
或
创建
一个账户,您的编辑将归属于您的用户名,且将享受其他好处。
反垃圾检查。
不要
加入这个!
__TOC__ __TOC__ 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主题。如果你想要学习更多的关于怎样安装和使用主题的知识,请查看关于[[WordPress:Using Themes|使用主题]]的文件。这个话题与[[WordPress:Using Themes|使用主题]]不同,因为主机发展探讨了编写代码来创建你自己的主题的技术方法,而不是激活主题,或者在哪儿能得到新的主题。 You may wish to develop WordPress Themes for your own use or [[WordPress:Designing Themes for Public Release|for distribution]]. ==Why WordPress Themes== 你可能因为自己需要使用主题,或者[[WordPress:Designing Themes for Public Release|因为发行]]而发展新主题。 ==为什么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 take advantage of [[WordPress:Stepping Into Templates|templates]], [[WordPress:Template Tags|template tags]], and [[WordPress:The Loop in Action|the WordPress Loop]] to generate different web page results and looks. *要利用[[WordPress:Stepping Into Templates|模板]], [[WordPress:Template Tags|模板标签]], 和 [[WordPress:The Loop in Action| WordPress Loop]]来产生不同的网页结果和外观。 * To provide alternative templates for specific site features, such as [[WordPress:Category Templates|category pages]] and search result pages. *为特别的站点功能提供可供选择的模板,例如[[WordPress:Category Templates|类别网页]]和搜索结果网页。 * To quickly switch between two site layouts, or to take advantage of a [[WordPress:Plugins|Theme or style switcher]] to allow users to change the look of your site. *快速地在两个站点布局之间进行切换,或者利用一个[[WordPress:Plugins|主题或者样式切换器]],允许用户改变你的站点的外观。 * 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 [[WordPress:Stepping Into Templates|template files]] from the system files so the site will upgrade without drastic changes to the visual presentation of the site. *主题将呈现样式和[[WordPress:Stepping Into Templates|模板文件]]从系统文件中分开,这样站点更新的时候,不会使站点的外观效果发生巨大的变化。 * 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). *这非常地有趣(大多数时候)。 * If you [[WordPress:Designing Themes for Public_Release|release it to the public]], you can feel good that you shared and gave something back to the [[WordPress:Contributing_to_WordPress|WordPress Community]] (okay, bragging rights!) *如果你[[WordPress:Designing Themes for Public_Release|将它公开发行]],你会感觉良好,因为你与WordPress团队一起分享你的主题,并且向[[WordPress:Contributing_to_WordPress|WordPress 团队]] 返回了你的贡献(好的,bragging rights!) ==Anatomy of a Theme== ==主题的解析== WordPress Themes live in subdirectories residing in <tt>wp-content/themes/</tt>. The Theme's subdirectory holds all of the Theme's style sheet files, [[WordPress:Stepping Into Templates|template files]], an optional functions file (<tt>functions.php</tt>), and images. For example, a Theme named "test" would probably reside in the directory <tt>wp-content/themes/test/</tt>. WordPress主题位于<tt>wp-content/themes/</tt>的子目录中。主题的子目录包含有主题的所有样式表文件,[[WordPress:Stepping Into Templates|模板文件]],一个可选择的的函数文件(<tt>functions.php</tt>),和图像。例如,一个叫做"测试"的主题可能会在<tt>wp-content/themes/test/</tt>目录中。 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 <tt>style.css</tt>, which controls the presentation (look) of the web pages. The second is the optional functions file (<tt>functions.php</tt>). The other files are the [[WordPress:Stepping Into Templates|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主题除了图像外,包括三种主要类型的文件。一种是样式表称为<tt>style.css</tt>,这控制了网页的外表(外观)。第二种是可选择的函数文件(<tt>functions.php</tt>)。其它的文件是[[WordPress:Stepping Into Templates|模板文件]],这个文件控制了数据库中的信息,在网页上的显示方式。让我们分别地看看这三种文件。 ===Theme Style Sheet=== ===主题样式表=== In addition to CSS style information for your theme, the stylesheet, <tt>style.css</tt> '''''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 [[WordPress:Administration_Panels#Design_-_Change_the_Look_of_your_Blog|Theme selection dialog]]. If you make your own Theme by copying an existing one, make sure you change this information first. 除了提供你的主题的CSS样式信息外,样式表,<tt>style.css</tt> '''''必须'''''提供评论形式的主题的详细信息。评论标题上不能列出'''''两个主题同样的详细信息''''',因为这会引起[[WordPress:Administration_Panels#Design_-_Change_the_Look_of_your_Blog|主题选择对话]]的错误。如果你通过复制一个现存的主题来制作你自己的主题,首先要确定你已经更改了这个信息。 The following is an example of the first few lines of the stylesheet, called the style sheet header, for the Theme "Rose": 下面是主题的"玫瑰"的样式表前几行的例子,称作样式表标题: <pre>/* 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. 如果有许可的话,一般的评论/许可声明。 */</pre> */</pre> The simplest Theme includes only a <tt>style.css</tt> 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 <tt>Template:</tt> line in the <tt>style.css</tt> header comments. For example, if you wanted the Theme "Rose" to inherit the templates from another Theme called "test", you would include <tt>Template: test</tt> in the comments at the beginning of Rose's <tt>style.css</tt>. Now "test" is the parent Theme for "Rose", which still consists only of a <tt>style.css</tt> file and the concomitant images, all located in the directory <tt>wp-content/themes/Rose</tt>. ''(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.)'' 如果存在最简单的主题,这种主题只包括一个<tt>style.css</tt>文件和图像(如果有图像)。要创建这样的一个主题,你必须通过在<tt>style.css</tt>标题评论中编辑<tt>Template:</tt>行,来规定一套模板来''继承''使用主题。例如,如果你想要"Rose"主题来继承其它的一个称作"测试"的主题的模板,你就会将评论中的<tt>Template: test</tt>放入Rose的tt>style.css</tt>开端。现在"test" 是"Rose"的母主题,只包含有一个<tt>style.css</tt>文件和一个相关的图像,都在<tt>wp-content/themes/Rose</tt>目录中。''(注规定一个母主题会从主题&mdash中继承''所有的''主题文件;意思是子主题目录中的任何模板文件都会被忽视。)'' The comment header lines in <tt>style.css</tt> are required for WordPress to be able to identify a Theme and display it in the [[WordPress:Administration_Panels|Administration Panel]] under [[WordPress:Administration_Panels#Design_-_Change_the_Look_of_your_Blog|Design]] > [[WordPress:Administration_Panels#Themes|Themes]] as an available Theme option along with any other installed Themes. <tt>style.css</tt>中的评论标题行要求WordPress能够辨别一个主题并且将主题显示在[[WordPress:Administration_Panels#Design_-_Change_the_Look_of_your_Blog|设计]] > [[WordPress:Administration_Panels#Themes|主题]]下的[[WordPress:Administration_Panels|管理面板]]与其它安装好的主题一起,作为可选择的主题。 '''Note''' : ''When defining the parent Theme, in the <tt>Template:</tt> 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 <tt>Template: WordPress Default</tt>, but <tt>Template: default</tt>, because default is the directory of this Theme.'' '''注''' : ''当在评论标题的<tt>Template:</tt>部分定义一个母主题的时候,你必须使用那个样式的目录名。例如,将默认的WordPress主题用作母主题,不要写成<tt>模板: WordPress 默认</tt>,要写成<tt>模板: 默认</tt>,因为默认是这个主题的目录。'' ===Functions File=== ===函数 文件=== A theme can optionally use a functions file, which resides in the theme subdirectory and is named <tt>functions.php</tt>. This file basically acts like a [[WordPress:Plugins|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: 主题可以随意地使用函数文件,这个文件在主题子目录中,并且命名为<tt>functions.php</tt>。文件基本像一个[[WordPress:Plugins|插件]]那样运行,而且如果文件呈现在你使用的主题中,会在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 <tt>functions.php</tt> file that defines functions and an admin screen, so you might want to use it as a model. Since <tt>functions.php</tt> 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主题包含一个<tt>functions.php</tt>文件,这个文件定义了函数和一个管理界面,这样你可能想将这个文件用作一个模型。因为<tt>functions.php</tt>基本起一个插件的作用,[[WordPress:Function_Reference|函数_参考]]列表有你怎样处理这个文件的更多的信息。 ===Theme Template Files=== ===主题模板文件=== [[WordPress:Stepping Into Templates|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. [[WordPress:Stepping Into Templates|模板]]是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 <tt>index.php</tt> 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能够使你为你的博客的不同部分分别定义模板;然而,使你的博客正常地运行,并不一定需要所有的这些不同的模板文件。模板是根据[[WordPress:Template Hierarchy|模板层级]]选择和产生的,取决于特别的主题中拥有什么样的模板。作为一个主题开发者,你可以选择你想使用模板,自定义多少内容。例如,一个极端的例子,你可以为网络日志上产生的和显示的''所有的''网页,只使用一个称为<tt>index.php</tt>模板文件,作为模板。一个更加通常的用法是使用不同的模板文件产生不同的结果,允许最大化的自定义。 ===='''Basic Templates'''==== ===='''基本的模板'''==== At the very minimum, a WordPress Theme consists of two files: 在最小值的时候, WordPress主题包含有两个文件: * <tt>style.css</tt> * <tt>index.php</tt> * <tt>style.css</tt> * <tt>index.php</tt> Both of these files go into the Theme's directory. The <tt>index.php</tt> [[WordPress:Stepping Into Templates|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. 这两个文件都归属于主题的目录。<tt>index.php</tt> [[WordPress:Stepping Into Templates|模板文件]]非常地灵活。它可以被用来包括关于标题,工具条,页脚,内容,类别,归档,搜索,错误和你的站点上的用户创建的其它的网页的所有参数。或者它可以被''再次地分''到模模板文件,每个部分都具有一部分的工作量。 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 <tt>comments.php</tt> or <tt>comments-popup.php</tt> template file, then WordPress will automatically use the <tt>wp-comments.php</tt> and <tt>wp-comments-popup.php</tt> 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会使用内置的默认文件。例如,如果你既没有<tt>comments.php</tt>也没有<tt>comments-popup.php</tt>模板文件,WordPress就会使用[[WordPress:Template Hierarchy|模板层级]],自动地使用<tt>wp-comments.php</tt> 和<tt>wp-comments-popup.php</tt>模板文件。这些默认模板可能不太匹配你的主题,因此你可能需要提供你自己的模板。用来再次地切分的基本的文件(归属于主题的目录)是: * <tt>header.php</tt> * <tt>sidebar.php</tt> * <tt>footer.php</tt> * <tt>comments.php</tt> * <tt>comments-popup.php</tt> * <tt>header.php</tt> * <tt>sidebar.php</tt> * <tt>footer.php</tt> * <tt>comments.php</tt> * <tt>comments-popup.php</tt> Using these modular template files, you can put template tags within the <tt>index.php</tt> master file to include or ''get'' these units where you want them to appear in the final generated web page. 使用这些模的模板文件,你可以将这些模板标签放到<tt>index.php</tt>主要文件中来包括或者''得到''这些units,你想让它们出现在最后产生的网页的位置。 * To include the header, use the <tt>[[WordPress:Include Tags#The Header Template|get_header()]]</tt> template tag. *要包含标题,请使用<tt>[[WordPress:Include Tags#The Header Template|get_header()]]</tt>模板标签。 * To include the sidebar, use the <tt>[[WordPress:Include Tags#The Sidebar Template|get_sidebar()]]</tt> template tag. *要包含工具条,使用<tt>[[WordPress:Include Tags#The Sidebar Template|get_sidebar()]]</tt>模板标签。 * To include the footer, use the <tt>[[WordPress:Include Tags#The Footer Template|get_footer()]]</tt> template tag. *要包含页脚,使用<tt>[[WordPress:Include Tags#The Footer Template|get_footer()]]</tt> 模板标签。 Here is an example of the ''include'' usage: 下面是关于''包含'' 用法的一个例子: <pre><?php get_sidebar(); ?> <?php get_footer(); ?></pre> <pre><?php get_sidebar(); ?> <?php get_footer(); ?></pre> For more on how these various Templates work and how to generate different information within them, read the [[WordPress:Templates]] documentation. 关于这些不同模板怎样运行以及怎样在模板里面产生不同信息的更多内容,请阅读[[WordPress:Templates|模板]]文件。 ===='''Query-based Templates'''==== ===='''以查询为基础的模板'''==== WordPress can load different [[WordPress:Stepping Into Templates|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可以为不同的''查询'' 形式载入不同的[[WordPress:Stepping Into Templates|模板]]。有两种方法可以做到这一点:作为内置的[[WordPress:Template Hierarchy|模板层级]]的一部分,通过在模板文件的[[WordPress:The Loop|Loop]]中使用[[WordPress:Conditional Tags|条件式标签]]。 To use the [[WordPress:Template Hierarchy]], you basically need to provide special-purpose Template files, which will automatically be used to override <tt>index.php</tt>. For instance, if your Theme provides a template called <tt>category.php</tt> and a category is being queried, <tt>category.php</tt> will be loaded instead of <tt>index.php</tt>. If <tt>category.php</tt> is not present, <tt>index.php</tt> is used as usual. 要使用[[WordPress:Template Hierarchy|模板层级]],你只要提供特殊用途的模板文件,这个文件会自动地覆盖<tt>index.php</tt>。例如,如果你的主题提供一个叫做<tt>category.php</tt>的模板,并且提供一个正被查询的类别,<tt>category.php</tt>而不是<tt>index.php</tt>将会被载入。如果<tt>category.php</tt>没有出现,<tt>index.php</tt>会照常使用。 You can get even more specific in the Template Hierarchy by providing a file called, for instance, <tt>category-6.php</tt> -- this file will be used rather than <tt>category.php</tt> when generating the page for the category whose ID number is 6. (You can find category ID numbers in [[WordPress:Administration_Panels#Manage_-_Change_your_content|Manage]] > [[WordPress:Administration_Panels#Categories|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]]. 通过提供一个例如,称作<tt>category-6.php</tt>的文件,你可以在模板层级中得到更加详细的信息---当为一个ID号码为6的类别产出网页的时候,这个文件而不是<tt>category.php</tt>会被用到。(如果你在WordPress2.3版本以及更低的版本中,以站点管理员的身份登录,你在 [[WordPress:Administration_Panels#Manage_-_Change_your_content|管理]] > [[WordPress:Administration_Panels#Categories|类别]] 下面会看到这个类别的ID数字。在WordPress 2.5版本中,管理面板中移除了ID栏。通过点击'编辑类别' ,并且查找URL地址中的cat_ID 参数值,你可以查找类别id的位置。看起来就像'...categories.php?action=edit&cat_ID=3' 其中'3'就是类别 id)。更详细地看看这个过程是怎样进行的,请看看[[WordPress:Category Templates|类别模板]]。 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:The_Loop_in_Action|WordPress Loop]], and then you can load a particular template, or put some particular text on the screen, based on that condition. 如果你的主题需要更多地控制使用的哪个模板文件而不是[[WordPress:Template Hierarchy|模板层级]]中所提供的内容,你可以使用[[WordPress:Template Hierarchy|条件式标签]]。条件式标签基本上是用来检查看看在[[WordPress:The_Loop_in_Action|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: 例如,给一个特别类别中的文章创建一个样式表,代码可能看起来像这样的: <pre><?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'); } ?></pre> <pre><?php if (is_category(9)) { //查找类别9中的文章 包括(TEMPLATEPATH . '/single2.php'); } else { // 每隔一篇文章便放入这个 include(TEMPLATEPATH . '/single1.php'); } ?></pre> Or, using a query, it might look like this: 或者,使用一个查询,这个查询可能看起来像: <pre><?php $post = $wp_query->post; if ( in_category('9') ) { include(TEMPLATEPATH . '/single2.php'); } else { include(TEMPLATEPATH . '/single1.php'); } ?></pre> <pre><?php $post = $wp_query->post; if ( in_category('9') ) { include(TEMPLATEPATH . '/single2.php'); } else { include(TEMPLATEPATH . '/single1.php'); } ?></pre> 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. 在上述任一例子中,代码会产生不同的模板,这些模板的使用取决于类别上显示的文章。查询条件并不局限于类别,然而—请看看[[WordPress:Conditional Tags|条件式标签]]文章来看看所有的选项。 === Media Icons === === 媒体图标 === '''This feature is [http://trac.wordpress.org/ticket/6751 currently broken in WordPress 2.5].''' '''这个功能[http://trac.wordpress.org/ticket/6751 当前在 WordPress 2.5中破坏了]。''' Wordpress uses media icons to represent [[WordPress:Using_Image_and_File_Attachments|attachment files]] on your blog and in the Admin interface, if those icons are available. WordPress使用媒体icons来代表你的博客和管理界面上的[[WordPress:Using_Image_and_File_Attachments|附属文件]],前提是存在这些图标。 It looks for image files named by media type in the <tt>images</tt> directory of the current theme. (As of Wordpress 2.2, the default theme comes with only one media icon, <tt>audio.jpg</tt>.) 它要查找当前主题的<tt>images</tt>目录中的媒体类型所命名的图像文件。(从Wordpress2.2开始,默认的主题只有一个媒体图标,<tt>audio.jpg</tt>。) For example, for an attachment of [http://en.wikipedia.org/wiki/Internet_media_type MIME type] <tt>audio/mpeg</tt>, Wordpress would look for an icon file at these locations, stopping after the first match (see <tt>[[WordPress:Function_Reference/wp_mime_type_icon|wp_mime_type_icon]]</tt>): 例如,对于[http://en.wikipedia.org/wiki/Internet_media_type MIME 类型] <tt>audio/mpeg</tt>的一个附属装置,WordPress在这些位置中寻找一个图标文件,在找到第一个匹配文件后,便停止(请看看<tt>[[WordPress:Function_Reference/wp_mime_type_icon|wp_mime_type_icon]]</tt>): # <var>my_theme</var>/images/audio.jpg # <var>my_theme</var>/images/audio.gif # <var>my_theme</var>/images/audio.png # <var>my_theme</var>/images/mpeg.jpg # <var>my_theme</var>/images/mpeg.gif # <var>my_theme</var>/images/mpeg.png # <var>my_theme</var>/images/audio_mpeg.jpg # <var>my_theme</var>/images/audio_mpeg.gif # <var>my_theme</var>/images/audio_mpeg.png # <var>my_theme</var>/images/audio.jpg # <var>my_theme</var>/images/audio.gif # <var>my_theme</var>/images/audio.png # <var>my_theme</var>/images/mpeg.jpg # <var>my_theme</var>/images/mpeg.gif # <var>my_theme</var>/images/mpeg.png # <var>my_theme</var>/images/audio_mpeg.jpg # <var>my_theme</var>/images/audio_mpeg.gif # <var>my_theme</var>/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具有一些特别的意义—更多的信息请看看[[WordPress:Template Hierarchy|模板层级]]。 '' ;<tt>style.css</tt>: The main stylesheet. This '''must''' be included with your Theme, and it must contain the information header for your Theme. ;<tt>style.css</tt>:主要的样式表。这个'''必须'''包含在你的主题中,而且必须包含你的主题的信息标题。 ;<tt>index.php</tt>: The main template. If your Theme provides its own templates, <tt>index.php</tt> must be present. ;<tt>index.php</tt>:主要的模板。如果你的主题提供自己的模板,<tt>index.php</tt>必须呈现。 ;<tt>comments.php</tt>: The comments template. If not present, <tt>comments.php</tt> from the "default" Theme is used. ;<tt>comments.php</tt>:评论模板。如果没有出现,来自"默认"主题的<tt>comments.php</tt>就会被使用。 ;<tt>comments-popup.php</tt>: The popup comments template. If not present, <tt>comments-popup.php</tt> from the "default" Theme is used. ;<tt>comments-popup.php</tt>:弹出的评论模板。如果没有出现,来自"默认"主题的<tt>comments-popup.php</tt>就被使用。 ;<tt>home.php</tt>: The home page template. ;<tt>home.php</tt>: 主页模板。 ;<tt>single.php</tt>: The single post template. Used when a single post is queried. For this and all other query templates, <tt>index.php</tt> is used if the query template is not present. ;<tt>single.php</tt>:单一的文章模板。当一篇单一的文章被查询的时候,便会用到这个模板。对于这个和其它的查询模板,如果查询模板没有出现,就会用到<tt>index.php</tt>。 ;<tt>page.php</tt>: The page template. Used when an individual [[WordPress:Pages|Page]] is queried. ;<tt>page.php</tt>:网页模板。当一个单独的[[WordPress:Pages|网页]]得到查询的时候,便会用到这个。 ;<tt>category.php</tt>: The [[WordPress:Category Templates|category template]]. Used when a category is queried. ;<tt>category.php</tt>: The [[WordPress:Category Templates|类别模板]]。当询问类别的时候,便会用到这个。 ;<tt>author.php</tt>: The [[WordPress:Author Templates|author template]]. Used when an author is queried. ;<tt>author.php</tt>:[[WordPress:Author Templates|作者模板]]。查询作者的时候,会使用到这个。 ;<tt>date.php</tt>: The date/time template. Used when a date or time is queried. Year, month, day, hour, minute, second. ;<tt>date.php</tt>:日期/时间模板。当查询日期或者时间的时候,便会用到这个模板。年,月,日,时,分,秒。 ;<tt>archive.php</tt>: The archive template. Used when a category, author, or date is queried. Note that this template will be overridden by <tt>category.php</tt>, <tt>author.php</tt>, and <tt>date.php</tt> for their respective query types. ;<tt>archive.php</tt>:归档模板。当查询一个类别,作者或者日期的时候,便会用到这个模板。注意这个模板会被<tt>category.php</tt>, <tt>author.php</tt>, 和<tt>date.php</tt>各自的查询形式覆盖。 ;<tt>search.php</tt>: The search results template. Used when a search is performed. ;<tt>404.php</tt>: The '''[[WordPress:Creating_an_Error_404_Page|404 Not Found]]''' template. Used when WordPress cannot find a post or page that matches the query. ;<tt>search.php</tt>:搜索结果模板。当执行一个搜索的时候,便会用到这个模板。 ;<tt>404.php</tt>:'''[[WordPress:Creating_an_Error_404_Page|404 没有找到]]'''模板。当WordPress找不到匹配查询的文章或者网页的时候,便会用到这个模板。 These files have a special meaning with regard to WordPress because they are used as a replacement for <tt>index.php</tt>, when available, according to the [[WordPress:Template Hierarchy]], and when the corresponding [[WordPress:Conditional Tags|Conditional Tag]] (a.k.a <tt>is_*();</tt> function) returns true. For example, if only a single post is being displayed, the <tt>[[WordPress:Conditional Tags#A Single Post Page|is_single()]]</tt> function returns 'true', and, if there is a <tt>single.php</tt> file in the active Theme, that template is used to generate the page. 这些文件有关于WordPress的特别意思因为这些文件用来作为<tt>index.php</tt>替代品,条件是这些文件存在,根据[[WordPress:Template Hierarchy|模板层级]],而且相应的[[WordPress:Conditional Tags|条件式标签]] (a.k.a <tt>is_*();</tt> 函数) 返回是真。例如,如果只显示一篇文章,<tt>[[WordPress:Conditional Tags#A Single Post Page|is_single()]]</tt>函数返回'真的',如果归档主题中有<tt>single.php</tt>文件,这个模板就用来创建网页。 ===Referencing Files From a Template=== ===参考模板中的文件=== The WordPress Default Theme (based on Michael Heilemann's [http://binarybonsai.com/kubrick/ Kubrick] layout for WordPress 1.2) provides a good example of how queries are mapped onto templates. WordPress默认主题(以Michael Heilemann的[http://binarybonsai.com/kubrick/ Kubrick]WordPress1.2布局为基础)提供了一个非常好的例子,关于查询是怎样映射进模板的。 The code <tt><?php bloginfo('template_directory'); ?></tt> 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. 代码<tt><?php bloginfo('template_directory'); ?></tt>将模板目录的URL嵌入了模板输出中。你可以将任何额外的URI信息添加到你的主题的参考文件的这个输出中。 The code <tt><?php bloginfo('stylesheet_directory'); ?></tt> 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. 代码<tt><?php bloginfo('stylesheet_directory'); ?></tt>嵌入了目录的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 <tt>images/</tt> directory in your Theme, you need only specify this relative directory in the CSS, like so: 注意用在样式表中的URIs与样式表相关,但是与指代样式表的网页并不相关。这就避免了在CSS文件中包含PHP代码来规定类别。例如,如果你的主题包含了一个<tt>images/</tt>目录,你只需要在CSS中规定这个相关的目录,像这样的: <pre style="font-size: 0.9em">h1 { background-image: URL(images/my_background.jpg); }</pre> <pre style="font-size: 0.9em">h1 { background-image: URL(images/my_background.jpg); }</pre> 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 <tt>template_redirect</tt> [[WordPress:Plugin API#Current Hooks For Actions|action hook]]. More information about creating plugins can be found in the [[WordPress:Plugin API]] reference. 可以使用WordPress插件系统来定义额外的模板,这种模板以你自己自定义的标准,显示。这个高级的功能通过使用<tt>template_redirect</tt> [[WordPress:Plugin API#Current Hooks For Actions|action hook]]可以得到实现。更多的关于创建插件的信息可以在[[WordPress:Plugin API|插件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添加功能(请看看[[WordPress:Plugin API|插件API]]得到更多的信息)。大多数Action Hooks是在WordPress核心PHP代码内部,因此你的主题不要为它们的运行准备任何特别标签。但是有几个Action Hooks的确需要呈现在你的主题上,为了使插件能够直接地在你的页眉,页脚,工具条,或者在网页上显示信息。下面是你需要的特别Action Hook模板标签列表: ; wp_head ; wp_head : Goes in the [[WordPress:Glossary#HTML|HTML]] <tt><head></tt> element of a theme; <tt>header.php</tt> template. Example plugin use: add javascript code. : 进入 [[WordPress:Glossary#HTML|HTML]] <tt><head></tt> 主题元素; <tt>header.php</tt> 模板。例子插件使用: 添加 javascript 代码。 : Usage: <tt><nowiki><?php do_action('wp_head'); ?></nowiki></tt> :''-or-'' <tt><nowiki><?php wp_head(); ?></nowiki></tt> : 用法: <tt><nowiki><?php do_action('wp_head'); ?></nowiki></tt> :''-or-'' <tt><nowiki><?php wp_head(); ?></nowiki></tt> ; wp_footer ; wp_footer : Goes in the "footer" of a theme; <tt>footer.php</tt> template. Example plugin use: insert PHP code that needs to run after everything else, at the bottom of the footer. : 进入主题的"页脚"中; <tt>footer.php</tt> 模板。例子插件使用: 在页脚的底部,嵌入需要在所有内容运行之后运行的PHP代码。 : Usage: <tt><nowiki><?php do_action('wp_footer'); ?></nowiki></tt> :''-or-'' <tt><nowiki><?php wp_footer(); ?></nowiki></tt> : 用法: <tt><nowiki><?php do_action('wp_footer'); ?></nowiki></tt> :''-or-'' <tt><nowiki><?php wp_footer(); ?></nowiki></tt> ; wp_meta ; wp_meta : Typically goes in the <tt><nowiki><li>Meta</li></nowiki></tt> section of a theme's menu or sidebar; <tt>sidebar.php</tt> template. Example plugin use: include a rotating advertisement or a tag cloud. : 通常进入主题的菜单或者工具条的<tt><nowiki><li>Meta</li></nowiki></tt>部分; <tt>sidebar.php</tt> 模板。 例子插件使用: 包括旋转的广告或者标签cloud。 : Usage: <tt><nowiki><?php do_action('wp_meta'); ?></nowiki></tt> :''-or-'' <tt><nowiki><?php wp_meta(); ?></nowiki></tt> : 用法: <tt><nowiki><?php do_action('wp_meta'); ?></nowiki></tt> :''-or-'' <tt><nowiki><?php wp_meta(); ?></nowiki></tt> ; comment_form ; comment_form : Goes in <tt>comments.php</tt> and <tt>comments-popup.php</tt>, directly before the comment form's closing tag (<code><nowiki></form></nowiki></code>). Example plugin use: display a comment preview. : 进入 <tt>comments.php</tt> 和<tt>comments-popup.php</tt>中, 直接在评论形式的关闭标签 (<code><nowiki></form></nowiki></code>)前面。 例子插件使用: 显示一个评论预览。 : Usage: <tt><nowiki><?php do_action('comment_form', $post->ID); ?></nowiki></tt> : 用法: <tt><nowiki><?php do_action('comment_form', $post->ID); ?></nowiki></tt> 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文件帮助许多用户克服了许多潜在的问题): # Indicate precisely what your Theme and template files will achieve. #明确地指出你的主题和模板文件要获得什么。 # Indicate deficiencies in your Themes, if any. #如果你的主题有什么缺陷的话,将它指出来。 # Clearly reference any special modifications in [[WordPress:Commenting_Code|comments]] within the template and style sheet files. Add comments to modifications, template sections, and CSS styles, especially those which cross template files. #明确地指出模板和样式表文件中[[WordPress:Commenting_Code|评论]]任何特别的修改。给更改,模板部分,CSS样式,特别是那些穿过模板文件的CSS样式添加评论。 # 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. #如果你有什么特别的要求,这些要求可能包括自定义RewriteRules,或者使用一些额外的,特别的模板,图像或者文件,请清楚地描述用户应该采取那些措施,以使你的主题运行。 # Try and test your Theme [[WordPress:CSS_Fixing_Browser_Bugs|across browsers]] to catch at least a few of the [[WordPress:CSS_Troubleshooting|problems]] the users of the Theme may find later. #试着测试你的主题[[WordPress:CSS_Fixing_Browser_Bugs|跨越浏览器]]至少发现几个,未来主题用户可能碰到的[[WordPress:CSS_Troubleshooting|问题]]。 # Provide contact information (web page or email), if possible, for support information and questions. #如果可能的话,提供联系信息(网页或者电子邮件),来支持信息和问题。 Take time to read through [[WordPress:Designing Themes for Public Release]], an article with good tips on preparing your Theme for the public. 花一些时间来通读[[WordPress:Designing Themes for Public Release|为了公共发行而设计主题]],这篇文章有许多关于怎样准备你的主题来公共发行的好的贴士。 ==References and Resources== ==参考和资源== There is a comprehensive list of WordPress Theme and Template File resources in the [[WordPress:Templates]] article. 在[[WordPress:Templates|模板]]文章中有WordPress主题和模板文件资源。
摘要:
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅
Wordpress-mediawiki:版权
的细节)。
未经许可,请勿提交受版权保护的作品!
取消
编辑帮助
(在新窗口中打开)