Theme Development:修訂間差異
(新頁面: __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...) ? |
無編輯摘要 ? |
||
第1行: | 第1行: | ||
__TOC__ | __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. | 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. | ||
下面的文章是關于發(fā)展或者設計你自己的WordPress主題。如果你想要學習更多的關于怎樣安裝和使用主題的知識,請查看關于[[WordPress:Using Themes|使用主題]]的文件。這個話題與[[WordPress:Using Themes|使用主題]]不同,因為主機發(fā)展探討了編寫代碼來創(chuàng)建你自己的主題的技術方法,而不是激活主題,或者在哪兒能得到新的主題。 | |||
You may wish to develop WordPress Themes for your own use or [[WordPress:Designing Themes for Public Release|for distribution]].? ? | You may wish to develop WordPress Themes for your own use or [[WordPress:Designing Themes for Public Release|for distribution]].? ? | ||
==Why WordPress Themes== | ==Why WordPress Themes== | ||
你可能因為自己需要使用主題,或者[[WordPress:Designing Themes for Public Release|因為發(fā)行]]而發(fā)展新主題。 | |||
==為什么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 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主題是文件和樣式的結合體,用來創(chuàng)建WordPress站點的外觀或者呈現方式。每個主題可能有所不同,為每個用戶提供許多選擇,用戶利用這些選擇來立即地更改它們的站點的外觀。你為什么要創(chuàng)建你自己的WordPress主題? | |||
* To create your own unique WordPress site look | * To create your own unique WordPress site look | ||
*創(chuàng)建你自己的獨特的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. | * 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. | * 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. | * 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. | * To design WordPress Theme(s) so that others may enjoy your designs through public release. | ||
*設計WordPress主題,公開發(fā)行,這樣其他人享用你的設計。 | |||
A WordPress Theme has many benefits, too. ? | A WordPress Theme has many benefits, too. ? | ||
WordPress主題也擁有許多的優(yōu)勢。 | |||
* 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. | * 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|模板文件]]從系統(tǒng)文件中分開,這樣站點更新的時候,不會使站點的外觀效果發(fā)生巨大的變化。 | |||
* It allows for customization of the presentation and web page results unique to that Theme. | * 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. | * 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. | * 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,以創(chuàng)建好的站點外觀。 | |||
Why should you build your own WordPress Theme?? That's the real question. | Why should you build your own WordPress Theme?? That's the real question. | ||
你為什么應該創(chuàng)建你自己的WordPress主題?這才是真正的問題所在。 | |||
* It's an opportunity to learn more about CSS, HTML/XHTML, and PHP. | * 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. | * 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 creative. | ||
*這具有創(chuàng)造性。 | |||
* It's fun (most of the time). | * 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!) | * 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|將它公開發(fā)行]],你會感覺良好,因為你與WordPress團隊一起分享你的主題,并且向[[WordPress:Contributing_to_WordPress|WordPress 團隊]] 返回了你的貢獻(好的,bragging rights!) | |||
==Anatomy of a Theme== | ==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 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 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 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=== | ===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. | 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": | The following is an example of the first few lines of the stylesheet, called the style sheet header, for the Theme "Rose": | ||
下面是主題的"玫瑰"的樣式表前幾行的例子,稱作樣式表標題: | |||
<pre>/*? ? | <pre>/*? ? | ||
第51行: | 第115行: | ||
Version: a-number--optional | Version: a-number--optional | ||
. | . | ||
<pre>/*? | |||
主題名: 玫瑰 | |||
主題 URI: 主題的主頁 | |||
描述: 主要的簡述 | |||
作者:你的姓名 | |||
作者URI: 你的-URI | |||
模板: 使用-這個-來-定義-一個-母-主題—任意的 | |||
版本: 一個-數字—隨意的。 | |||
General comments/License Statement if any. | General comments/License Statement if any. | ||
? | |||
如果有許可的話,一般的評論/許可聲明。 | |||
? | |||
*/</pre> | |||
? | |||
*/</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.)'' | 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>文件和圖像(如果有圖像)。要創(chuàng)建這樣的一個主題,你必須通過在<tt>style.css</tt>標題評論中編輯<tt>Template:</tt>行,來規(guī)定一套模板來''繼承''使用主題。例如,如果你想要"Rose"主題來繼承其它的一個稱作"測試"的主題的模板,你就會將評論中的<tt>Template: test</tt>放入Rose的tt>style.css</tt>開端?,F在"test" 是"Rose"的母主題,只包含有一個<tt>style.css</tt>文件和一個相關的圖像,都在<tt>wp-content/themes/Rose</tt>目錄中。''(注規(guī)定一個母主題會從主題&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. | 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.'' | '''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=== | ===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: | 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 | * 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 | * 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. | 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=== | ===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|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 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|模板層級]]選擇和產生的,取決于特別的主題中擁有什么樣的模板。作為一個主題開發(fā)者,你可以選擇你想使用模板,自定義多少內容。例如,一個極端的例子,你可以為網絡日志上產生的和顯示的''所有的''網頁,只使用一個稱為<tt>index.php</tt>模板文件,作為模板。一個更加通常的用法是使用不同的模板文件產生不同的結果,允許最大化的自定義。 | |||
===='''Basic Templates'''==== | ===='''Basic Templates'''==== | ||
===='''基本的模板'''==== | |||
At the very minimum, a WordPress Theme consists of two files: | 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>style.css</tt> | ||
第83行: | 第191行: | ||
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. | 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|模板文件]]非常地靈活。它可以被用來包括關于標題,工具條,頁腳,內容,類別,歸檔,搜索,錯誤和你的站點上的用戶創(chuàng)建的其它的網頁的所有參數?;蛘咚梢员?'再次地分''到模模板文件,每個部分都具有一部分的工作量。 | |||
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: | 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>header.php</tt> | ||
第92行: | 第211行: | ||
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. | 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. | * 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. | * 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. | * 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: | Here is an example of the ''include'' usage: | ||
下面是關于''包含'' 用法的一個例子: | |||
<pre><?php get_sidebar(); ?> | |||
<?php get_footer(); ?></pre> | |||
<pre><?php get_sidebar(); ?> | <pre><?php get_sidebar(); ?> | ||
第104行: | 第241行: | ||
For more on how these various Templates work and how to generate different information within them, read the [[WordPress:Templates]] documentation. | 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'''==== | ===='''Query-based Templates'''==== | ||
===='''以查詢?yōu)榛A的模板'''==== | |||
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 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. | 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]]. | 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. | 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: | For example, to generate a distinctive style sheet in a post only found within a specific category, the code might look like this: | ||
例如,給一個特別類別中的文章創(chuàng)建一個樣式表,代碼可能看起來像這樣的: | |||
<pre><?php | <pre><?php | ||
第123行: | 第274行: | ||
} else { | } else { | ||
?? // put this on every other category post | ?? // 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'); | ?? include(TEMPLATEPATH . '/single1.php'); | ||
} | } | ||
第128行: | 第289行: | ||
Or, using a query, it might look like this: | 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 | <pre><?php | ||
第139行: | 第311行: | ||
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. | 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 === | === Media Icons === | ||
=== 媒體圖標 === | |||
'''This feature is [http://trac.wordpress.org/ticket/6751 currently broken in WordPress 2.5].''' | '''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 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>.) | 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>): | 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.jpg | ||
第161行: | 第358行: | ||
===Theme Template Files List=== | ===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.'' | 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>: 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>: 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>: 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>: 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>: 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>: 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>: 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|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>: 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>: 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>: 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>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>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>:搜索結果模板。當執(zhí)行一個搜索的時候,便會用到這個模板。 | |||
;<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. | 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>文件,這個模板就用來創(chuàng)建網頁。 | |||
===Referencing Files From a Template=== | ===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. | 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. | 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. | 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). | 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:? ? | 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代碼來規(guī)定類別。例如,如果你的主題包含了一個<tt>images/</tt>目錄,你只需要在CSS中規(guī)定這個相關的目錄,像這樣的: | |||
<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> | <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. | 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=== | ===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. | 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插件系統(tǒng)來定義額外的模板,這種模板以你自己自定義的標準,顯示。這個高級的功能通過使用<tt>template_redirect</tt> [[WordPress:Plugin API#Current Hooks For Actions|action hook]]可以得到實現。更多的關于創(chuàng)建插件的信息可以在[[WordPress:Plugin API|插件API]]參考中找到。 | |||
===Plugin API Hooks=== | ===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: | 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: | ||
發(fā)展主題的時候,最好牢記你的主題得到了設置,這樣它就能夠很好地配合你(或者其它的主題用戶)可能決定安裝的WordPress插件。插件通過"Action Hooks"來給WordPress添加功能(請看看[[WordPress:Plugin API|插件API]]得到更多的信息)。大多數Action Hooks是在WordPress核心PHP代碼內部,因此你的主題不要為它們的運行準備任何特別標簽。但是有幾個Action Hooks的確需要呈現在你的主題上,為了使插件能夠直接地在你的頁眉,頁腳,工具條,或者在網頁上顯示信息。下面是你需要的特別Action Hook模板標簽列表: | |||
; wp_head ? | ; 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. ? | : 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> ? | : Usage: <tt><nowiki><?php do_action('wp_head'); ?></nowiki></tt> ? | ||
:''-or-'' <tt><nowiki><?php 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 | ; 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. | : 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> ? | : Usage: <tt><nowiki><?php do_action('wp_footer'); ?></nowiki></tt> ? | ||
:''-or-'' <tt><nowiki><?php 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 ? | ; 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. | : 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> ? | : Usage: <tt><nowiki><?php do_action('wp_meta'); ?></nowiki></tt> ? | ||
:''-or-'' <tt><nowiki><?php 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 ? | ; 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. | : 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> ? | : 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. | For a real world usage example, you'll find these plugin hooks included in the default theme's templates. | ||
對于真實的世界用法的例子,你將會發(fā)現這些插件hooks包含在默認主題的模板中。 | |||
==Theme Development General Guidelines== | ==Theme Development General Guidelines== | ||
==主題發(fā)展一般指導方針== | |||
Please be clear about the following in your documentation (a README file included with your Theme helps many users over any potential stumbling blocks): | 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 precisely what your Theme and template files will achieve. | ||
#明確地指出你的主題和模板文件要獲得什么。 | |||
# Indicate deficiencies in your Themes, if any. | # 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. ? | # 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. | # 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. | # 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|跨越瀏覽器]]至少發(fā)現幾個,未來主題用戶可能碰到的[[WordPress:CSS_Troubleshooting|問題]]。 | |||
# Provide contact information (web page or email), if possible, for support information and questions. | # 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. | 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|為了公共發(fā)行而設計主題]],這篇文章有許多關于怎樣準備你的主題來公共發(fā)行的好的貼士。 | |||
==References and Resources== | ==References and Resources== | ||
==參考和資源== | |||
There is a comprehensive list of WordPress Theme and Template File resources in the [[WordPress:Templates]] article. | There is a comprehensive list of WordPress Theme and Template File resources in the [[WordPress:Templates]] article. | ||
在[[WordPress:Templates|模板]]文章中有WordPress主題和模板文件資源。 |
2008年8月18日 (一) 16:56的最新版本
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.
下面的文章是關于發(fā)展或者設計你自己的WordPress主題。如果你想要學習更多的關于怎樣安裝和使用主題的知識,請查看關于使用主題的文件。這個話題與使用主題不同,因為主機發(fā)展探討了編寫代碼來創(chuàng)建你自己的主題的技術方法,而不是激活主題,或者在哪兒能得到新的主題。
You may wish to develop WordPress Themes for your own use or for distribution.
Why WordPress Themes[ ]
你可能因為自己需要使用主題,或者因為發(fā)行而發(fā)展新主題。
為什么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主題是文件和樣式的結合體,用來創(chuàng)建WordPress站點的外觀或者呈現方式。每個主題可能有所不同,為每個用戶提供許多選擇,用戶利用這些選擇來立即地更改它們的站點的外觀。你為什么要創(chuàng)建你自己的WordPress主題?
- To create your own unique WordPress site look
- 創(chuàng)建你自己的獨特的WordPress站點外觀
- To take advantage of templates, template tags, and the WordPress Loop to generate different web page results and looks.
- 要利用模板, 模板標簽, 和 WordPress Loop來產生不同的網頁結果和外觀。
- 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主題,公開發(fā)行,這樣其他人享用你的設計。
A WordPress Theme has many benefits, too. WordPress主題也擁有許多的優(yōu)勢。
- 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.
- 主題將呈現樣式和模板文件從系統(tǒng)文件中分開,這樣站點更新的時候,不會使站點的外觀效果發(fā)生巨大的變化。
- 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,以創(chuàng)建好的站點外觀。
Why should you build your own WordPress Theme? That's the real question.
你為什么應該創(chuàng)建你自己的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.
- 這具有創(chuàng)造性。
- It's fun (most of the time).
- 這非常地有趣(大多數時候)。
- If you release it to the public, you can feel good that you shared and gave something back to the WordPress Community (okay, bragging rights!)
- 如果你將它公開發(fā)行,你會感覺良好,因為你與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文件和圖像(如果有圖像)。要創(chuàng)建這樣的一個主題,你必須通過在style.css標題評論中編輯Template:行,來規(guī)定一套模板來繼承使用主題。例如,如果你想要"Rose"主題來繼承其它的一個稱作"測試"的主題的模板,你就會將評論中的Template: test放入Rose的tt>style.css開端?,F在"test" 是"Rose"的母主題,只包含有一個style.css文件和一個相關的圖像,都在wp-content/themes/Rose目錄中。(注規(guī)定一個母主題會從主題&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能夠使你為你的博客的不同部分分別定義模板;然而,使你的博客正常地運行,并不一定需要所有的這些不同的模板文件。模板是根據模板層級選擇和產生的,取決于特別的主題中擁有什么樣的模板。作為一個主題開發(fā)者,你可以選擇你想使用模板,自定義多少內容。例如,一個極端的例子,你可以為網絡日志上產生的和顯示的所有的網頁,只使用一個稱為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 模板文件非常地靈活。它可以被用來包括關于標題,工具條,頁腳,內容,類別,歸檔,搜索,錯誤和你的站點上的用戶創(chuàng)建的其它的網頁的所有參數。或者它可以被再次地分到模模板文件,每個部分都具有一部分的工作量。
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.php 和wp-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,你想讓它們出現在最后產生的網頁的位置。
- To include the header, use the get_header() template tag.
- 要包含標題,請使用get_header()模板標簽。
- To include the sidebar, use the get_sidebar() template tag.
- 要包含工具條,使用get_sidebar()模板標簽。
- To include the footer, use the get_footer() template tag.
- 要包含頁腳,使用get_footer() 模板標簽。
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[ ]
以查詢?yōu)榛A的模板[ ]
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:
例如,給一個特別類別中的文章創(chuàng)建一個樣式表,代碼可能看起來像這樣的:
<?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 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):
- my_theme/images/audio.jpg
- my_theme/images/audio.gif
- my_theme/images/audio.png
- my_theme/images/mpeg.jpg
- my_theme/images/mpeg.gif
- my_theme/images/mpeg.png
- my_theme/images/audio_mpeg.jpg
- my_theme/images/audio_mpeg.gif
- my_theme/images/audio_mpeg.png
- my_theme/images/audio.jpg
- my_theme/images/audio.gif
- my_theme/images/audio.png
- my_theme/images/mpeg.jpg
- my_theme/images/mpeg.gif
- my_theme/images/mpeg.png
- my_theme/images/audio_mpeg.jpg
- my_theme/images/audio_mpeg.gif
- 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
- 搜索結果模板。當執(zhí)行一個搜索的時候,便會用到這個模板。
- 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文件,這個模板就用來創(chuàng)建網頁。
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代碼來規(guī)定類別。例如,如果你的主題包含了一個images/目錄,你只需要在CSS中規(guī)定這個相關的目錄,像這樣的:
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插件系統(tǒng)來定義額外的模板,這種模板以你自己自定義的標準,顯示。這個高級的功能通過使用template_redirect action hook可以得到實現。更多的關于創(chuàng)建插件的信息可以在插件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:
發(fā)展主題的時候,最好牢記你的主題得到了設置,這樣它就能夠很好地配合你(或者其它的主題用戶)可能決定安裝的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.php 和comments-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.
對于真實的世界用法的例子,你將會發(fā)現這些插件hooks包含在默認主題的模板中。
Theme Development General Guidelines[ ]
主題發(fā)展一般指導方針[ ]
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 comments within the template and style sheet files. Add comments to modifications, template sections, and CSS styles, especially those which cross template files.
- 明確地指出模板和樣式表文件中評論任何特別的修改。給更改,模板部分,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 across browsers to catch at least a few of the problems the users of the Theme may find later.
- 試著測試你的主題跨越瀏覽器至少發(fā)現幾個,未來主題用戶可能碰到的問題。
- 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.
花一些時間來通讀為了公共發(fā)行而設計主題,這篇文章有許多關于怎樣準備你的主題來公共發(fā)行的好的貼士。
References and Resources[ ]
參考和資源[ ]
There is a comprehensive list of WordPress Theme and Template File resources in the WordPress:Templates article.
在模板文章中有WordPress主題和模板文件資源。