久久精品水蜜桃av综合天堂,久久精品丝袜高跟鞋,精品国产肉丝袜久久,国产一区二区三区色噜噜,黑人video粗暴亚裔

WordPress: Tag Templates:修訂間差異

來自站長百科
跳轉(zhuǎn)至: 導(dǎo)航、? 搜索
(新頁面: = Introduction = Since the advent of Themes in WordPress 1.5, changing the look and feel of your WordPress site has become fairly straightforward. For instance,...)
?
無編輯摘要
?
第1行: 第1行:
= Introduction =
= Introduction =
= 介紹 =
Since the advent of [[WordPress:Using Themes|Themes]] in WordPress 1.5, changing the look and feel of your WordPress site has become fairly straightforward. For instance, when a viewer clicks on a link to one of the Categories on your site, he or she is taken to a page listing the Posts from that particular Category in chronological order, from newest Posts at the top to oldest at the bottom. There are many display choices, including whether to display the complete post or post excerpts, and what additional information to display (title, author, publish date, last modified time, etc.). Each theme makes different choices, and you might want to change them. ?
Since the advent of [[WordPress:Using Themes|Themes]] in WordPress 1.5, changing the look and feel of your WordPress site has become fairly straightforward. For instance, when a viewer clicks on a link to one of the Categories on your site, he or she is taken to a page listing the Posts from that particular Category in chronological order, from newest Posts at the top to oldest at the bottom. There are many display choices, including whether to display the complete post or post excerpts, and what additional information to display (title, author, publish date, last modified time, etc.). Each theme makes different choices, and you might want to change them. ?
自從WordPress1.5出現(xiàn)了[[WordPress:Using Themes|主題]],更改你的WordPress站點的外觀和整體效果變得輕而易舉。例如,當訪客點擊你的站點上其中的一個類別鏈接的時候,他或者她就會來到一個頁面,這個頁面以時間順序列出了那個類別的所有文章,將最新發(fā)表的文章列在頂上方,將最遲發(fā)表的文章列在最下方。可以以許多不同的方式顯示文章,包括是否顯示完整的文章還是顯示摘錄,顯示哪些額外信息(標題,作者,發(fā)表日期,最后更改的時間,等等)。每個主題會有許多不同的選擇,你可能想要更改這些選項。


This article explains how to change what happens when the blog viewer is visiting one of your site's Category pages. This involves the use of Themes and Template files, so if you are new to template files, you might want to read [[WordPress:Using Themes]] and [[WordPress:Stepping Into Templates]] first.
This article explains how to change what happens when the blog viewer is visiting one of your site's Category pages. This involves the use of Themes and Template files, so if you are new to template files, you might want to read [[WordPress:Using Themes]] and [[WordPress:Stepping Into Templates]] first.
這篇文章解釋了,當博客訪客瀏覽你的站點的其中一個類別頁面的時候,怎樣更改出現(xiàn)的變化。這包括使用主題和主題文件,因此,如果你剛剛接觸模板文件,你可能想要先閱讀[[WordPress:Using Themes|使用主題]]和 [[WordPress:Stepping Into Templates|走進模板]]。


== What Template File is Used? == ?
== What Template File is Used? == ?
== 使用什么模板文件? ==


The first step in modifying what happens when someone visits a Category page is to figure out which of your theme's files is going to be used to display the posts. This is known as the [[WordPress:Template Hierarchy]]. ?
The first step in modifying what happens when someone visits a Category page is to figure out which of your theme's files is going to be used to display the posts. This is known as the [[WordPress:Template Hierarchy]]. ?
當有人瀏覽類別頁面的時候,更改發(fā)生變化的第一步是要了解,將要使用你的哪個主題文件來顯示文章。這也稱為[[WordPress:Template Hierarchy|模板層級]]。


In the case of categories, the hierarchy is fairly simple. For instance, suppose the ID number of the Category in question is '''6'''.? The Template Hierarchy specifies that WordPress will use the ''first'' Template file it finds in your current Theme's directory from the following list:
In the case of categories, the hierarchy is fairly simple. For instance, suppose the ID number of the Category in question is '''6'''.? The Template Hierarchy specifies that WordPress will use the ''first'' Template file it finds in your current Theme's directory from the following list:
#<tt style="font-weight:bold; color:#036">category-6.php</tt>
#<tt style="font-weight:bold; color:#036">category.php</tt>
#<tt style="font-weight:bold; color:#036">archive.php</tt>
#<tt style="font-weight:bold; color:#036">index.php</tt>
關(guān)于類別,層級非常簡單。例如,假如討論的類別ID的數(shù)字是'''6'''。模板層級規(guī)定WordPress將會使用WordPress在下面的列表,你的當前主題目錄中找到的''第一個''模板文件:
#<tt style="font-weight:bold; color:#036">category-6.php</tt>
#<tt style="font-weight:bold; color:#036">category-6.php</tt>
#<tt style="font-weight:bold; color:#036">category.php</tt>
#<tt style="font-weight:bold; color:#036">category.php</tt>
第15行: 第32行:


That is, if you do not have a <tt style="font-weight:bold; color:#036">category-6.php</tt>, WordPress will check for a <tt style="font-weight:bold; color:#036">category.php</tt>, and so on. ?
That is, if you do not have a <tt style="font-weight:bold; color:#036">category-6.php</tt>, WordPress will check for a <tt style="font-weight:bold; color:#036">category.php</tt>, and so on. ?
也就是說,如果你沒有<tt style="font-weight:bold; color:#036">category-6.php</tt>,WordPress就會查看<tt style="font-weight:bold; color:#036">category.php</tt>,等等。


So, if you want to make the Category whose ID number is 6 look different from what it is currently (and different from other Category pages), you would want to create a <tt>category-6.php</tt> file. If you want to make all Category pages look different from other archive pages (such as date and author archives), then you would want to create or modify the <tt>category.php file</tt>. If you want to make changes to the look of all archive pages, you can create or modify the <tt>archive.php</tt> file. And if you modify the <tt>index.php</tt> file, you will affect your entire blog.
So, if you want to make the Category whose ID number is 6 look different from what it is currently (and different from other Category pages), you would want to create a <tt>category-6.php</tt> file. If you want to make all Category pages look different from other archive pages (such as date and author archives), then you would want to create or modify the <tt>category.php file</tt>. If you want to make changes to the look of all archive pages, you can create or modify the <tt>archive.php</tt> file. And if you modify the <tt>index.php</tt> file, you will affect your entire blog.
因此,如果你希望類別ID是6的類別看起來與當前類別頁面不同(也與其它類別頁面不同),你可能想要創(chuàng)建<tt>category-6.php</tt>文件。如果你希望所有的類別頁面與其它歸檔頁面看起來不同(例如日期和作者歸檔),那么你可能想要創(chuàng)建或者更改<tt>category.php file</tt>。如果你想要更改所有的歸檔頁面的外觀,你可以創(chuàng)建或者更改<tt>archive.php</tt>文件。而且如果你更改了<tt>index.php</tt>文件,你就會影響你的整個博客。


If you need to create a new file, it is a good idea to copy it from the next file in the hierarchy that exists. For instance, if you want a special display for Category 6, begin by copying the <tt>category.php</tt> file, or if you don't have one, use <tt>archive.php</tt>, and so on.
If you need to create a new file, it is a good idea to copy it from the next file in the hierarchy that exists. For instance, if you want a special display for Category 6, begin by copying the <tt>category.php</tt> file, or if you don't have one, use <tt>archive.php</tt>, and so on.
如果你需要創(chuàng)建新的文件,最后從存在的層級中的另一個文件中復(fù)制這個文件。例如,如果你想要特別地顯示類別6,首先復(fù)制<tt>category.php</tt>文件,或者如果你沒有這個文件,請使用<tt>archive.php</tt>,等等。


= Examples =
= Examples =


= 例子 =
Now that you've figured out which template file in your theme's directory you need to modify, in order to make changes to the look of Category pages, let's look at some examples. In these examples, when it says "edit your template file", it means to edit the file you chose in the section above.
Now that you've figured out which template file in your theme's directory you need to modify, in order to make changes to the look of Category pages, let's look at some examples. In these examples, when it says "edit your template file", it means to edit the file you chose in the section above.
既然現(xiàn)在你已經(jīng)了解需要更改你的主題目錄中的哪個模板文件,為了更改類別頁面的外觀,讓我們看看一些例子。在這些例子中,當顯示"編輯你的模板文件"的時候,意思是編輯你從上面的部分中選擇的文件。


== Adding Text to Category Pages ==
== Adding Text to Category Pages ==
== 將文本添加到類別頁面==


=== Static Text Above Posts ===
=== Static Text Above Posts ===
=== 文章上方的靜態(tài)文本===


Suppose you want some static text displayed before the list of Posts on your Category page(s). By "static", we mean text that remains the same, no matter which posts will be displayed below, and no matter which category is being displayed. Here is how to do it: above [[WordPress:The Loop]] section of your Template file, insert the following code:
Suppose you want some static text displayed before the list of Posts on your Category page(s). By "static", we mean text that remains the same, no matter which posts will be displayed below, and no matter which category is being displayed. Here is how to do it: above [[WordPress:The Loop]] section of your Template file, insert the following code:
假如你希望你的類別頁面的文章列表之前顯示一些靜態(tài)文本。"靜態(tài)",指的是,不管下面顯示哪篇文章,文本都保持一致。下面是怎樣顯示靜態(tài)文本的操作:在你的模板文件的[[WordPress:The Loop|The Loop]]部分的上面,插入以下的代碼:
<pre><nowiki>
<pre><nowiki>
<p>
<p>
This is some text that will display at the top of the Category page.
This is some text that will display at the top of the Category page.
</p></nowiki></pre>
<pre><nowiki>
<p>
這些文本將會顯示在類別頁面的頂上方。
</p></nowiki></pre>
</p></nowiki></pre>


=== Different Text on Some Category Pages ===
=== Different Text on Some Category Pages ===


===一些類別頁面上的不同的文本 ===
A slightly more complex possibility is that you want different text to display depending on which category page the visitor is viewing. Then you would add the "default" text to the main <tt>category.php</tt> file, and create special <tt>category-#.php</tt> files (with their own version of the text, as described in the Introduction) for each category that needs special text at the top. ?
A slightly more complex possibility is that you want different text to display depending on which category page the visitor is viewing. Then you would add the "default" text to the main <tt>category.php</tt> file, and create special <tt>category-#.php</tt> files (with their own version of the text, as described in the Introduction) for each category that needs special text at the top. ?
稍微復(fù)雜的情況是,根據(jù)訪客瀏覽不同的類別頁面,你希望顯示不同的文本。那么你將要將"默認"文本添加到主要的<tt>category.php</tt>文件,并且為需要在頂上方出現(xiàn)特別文本的每個類別,創(chuàng)建特別的<tt>category-#.php</tt>文件(帶有自己版本的文本,如介紹中所描述的)。


This does however create a lot of files in your theme directory, and can be avoided using the following code '''OUTSIDE''' the loop:
This does however create a lot of files in your theme directory, and can be avoided using the following code '''OUTSIDE''' the loop:
這樣,在你的主題目錄中創(chuàng)建了許多文件,而且可以避免在loop '''外'''使用下面的代碼:


<pre><nowiki>
<pre><nowiki>
第48行: 第91行:
<p>This is some generic text to describe all other category pages, ?
<p>This is some generic text to describe all other category pages, ?
I could be left blank</p>
I could be left blank</p>
<?php } ?>
</nowiki></pre>
<pre><nowiki>
<?php if (is_category('Category A')) { ?>
<p>這是描述類別A的文本</p>
<?php } elseif (is_category('Category B')) { ?>
<p>這是描述類別B的文本</p>
<?php } else { ?>
<p>這是描述所有其它類別頁面的一般文本,我可以保留為空白
</p>
<?php } ?>
<?php } ?>
</nowiki></pre>
</nowiki></pre>


This does the following. Checks to see if we are looking at Category A, if we are then show the first bit of text, but if we're not then check if we are looking at Category B. If we are then show that bit of text, and finally, if it is neither Category A or B, then show this default text.
This does the following. Checks to see if we are looking at Category A, if we are then show the first bit of text, but if we're not then check if we are looking at Category B. If we are then show that bit of text, and finally, if it is neither Category A or B, then show this default text.
這個代碼執(zhí)行以下的操作。查看我們是否在查找類別A,如果我們是在查找類別A,我們會顯示一些文本,如果我們不是在查找類別A,代碼就會查看我們是否在查找類別B。如果我們那時顯示那點文本,而且最后,如果既不是類別A也不是類別B,那么就顯示這個默認文本。


=== Text Displaying Only on First Page of Archive ===
=== Text Displaying Only on First Page of Archive ===
=== 只在歸檔的第一個頁面上顯示文本===


Another thing that can happen is that if your Category contains more posts than will fit on one page (according to the Options for Blog Reading you have set in the Administration panels of your blog), the category archive will split into multiple pages. And maybe you only want to display your static text if the viewer is on the first page of results, or you want to display different text for the other pages.
Another thing that can happen is that if your Category contains more posts than will fit on one page (according to the Options for Blog Reading you have set in the Administration panels of your blog), the category archive will split into multiple pages. And maybe you only want to display your static text if the viewer is on the first page of results, or you want to display different text for the other pages.
另一種情況是,如果你的類別包含的文章數(shù)目超過了一個頁面上可以包含的文章數(shù)目(根據(jù)你在你的博客的管理面板上設(shè)置的博客閱讀選項),類別歸檔將會分為幾個頁面。如果訪客在第一個頁面上,你可能只想要顯示你的靜態(tài)文本,或者你想要為其它的頁面顯示不同的文本。


To make this happen, you can use a PHP <tt>if</tt> statement, which looks at the value of the <tt>$paged</tt> WordPress variable (<tt>$paged</tt> is equal to the page number: 1 for the first page of results, 2 for the second page, etc.). It may sound complicated, but it's actually not too bad. Just put the following above [[WordPress:The Loop]]:
To make this happen, you can use a PHP <tt>if</tt> statement, which looks at the value of the <tt>$paged</tt> WordPress variable (<tt>$paged</tt> is equal to the page number: 1 for the first page of results, 2 for the second page, etc.). It may sound complicated, but it's actually not too bad. Just put the following above [[WordPress:The Loop]]:
使得這種情況發(fā)生,你可以使用PHP<tt>if</tt>聲明,查看<tt>$paged</tt>WordPress變數(shù)的參數(shù)值(<tt>$paged</tt>與頁面數(shù)字相等:1代表結(jié)果的第一個頁面,2代表第二個頁面,等等)。聽起來復(fù)雜,但是事實上并不太難。只要將下面的代碼放到[[WordPress:The Loop|The Loop]]的上面:
<pre>
<pre>
<?php if ( $paged < 2 ) { ?>
<?php if ( $paged < 2 ) { ?>
第64行: 第129行:
<p>Text for subsequent pages of Category.
<p>Text for subsequent pages of Category.
Can be left out.</p>
Can be left out.</p>
<?php } ?>
</pre>
<pre>
<?php if ( $paged < 2 ) { ?>
<p>類別歸檔的第一個頁面的文本。</p>
<?php }其它的{ ?>
<p>類別的其它頁面的文本。可以留為空白。</p>
<?php } ?>
<?php } ?>
</pre>
</pre>


=== Category Name ===
=== Category Name ===
===類別名===


Another possibility is to put the category name at the top of the page. If this is not already part of your template, you can add it by doing something like this, above [[WordPress:The Loop]]:
Another possibility is to put the category name at the top of the page. If this is not already part of your template, you can add it by doing something like this, above [[WordPress:The Loop]]:
<pre>
<pre>
<p>Category: <?php single_cat_title(); ?></p>
<p>Category: <?php single_cat_title(); ?></p>
</pre>
另一種情況是將類別名放到頁面的頂上方。如果這還不是你的模板的一部分,你可以在[[WordPress:The Loop|The Loop]]上執(zhí)行這個操作,將這個部分添加到你的模板上:
<pre>
<p>類別: <?php single_cat_title(); ?></p>
</pre>
</pre>


== Modifying How Posts are Displayed ==
== Modifying How Posts are Displayed ==
== 更改文章的顯示方式 ==


=== Excerpts vs. Full Posts ===
=== Excerpts vs. Full Posts ===
=== 摘錄 vs. 完整的文章 ===


Perhaps you are looking to cut down on the size of your Category pages.? You could do this by displaying excerpts rather than the entire content of each Post. To do this, you will just need to find where it says <tt style="font-weight:bold; color:#036">[[WordPress:Template Tags/the_content|the_content()]]</tt> inside [[WordPress:The Loop]] in your Template, and replace it with <tt style="font-weight:bold; color:#036">[[WordPress:Template Tags/the_excerpt|the_excerpt()]]</tt>. These will most likely be inside PHP tags:
Perhaps you are looking to cut down on the size of your Category pages.? You could do this by displaying excerpts rather than the entire content of each Post. To do this, you will just need to find where it says <tt style="font-weight:bold; color:#036">[[WordPress:Template Tags/the_content|the_content()]]</tt> inside [[WordPress:The Loop]] in your Template, and replace it with <tt style="font-weight:bold; color:#036">[[WordPress:Template Tags/the_excerpt|the_excerpt()]]</tt>. These will most likely be inside PHP tags:
第82行: 第167行:
?? <?php the_excerpt(); ?>
?? <?php the_excerpt(); ?>
and
and
? <?php the_content(); ?>
</pre>
也許你想要更改你的類別頁面的大小。顯示摘錄而不是每篇文章的整個內(nèi)容,你就可以做到這一點。你只要在你的模板的[[WordPress:The Loop|The Loop]]內(nèi)找到顯示為<tt style="font-weight:bold; color:#036">[[WordPress:Template Tags/the_content|the_content()]]</tt>的位置,并將其更改為<tt style="font-weight:bold; color:#036">[[WordPress:Template Tags/the_excerpt|the_excerpt()]]</tt>,即可。這些很有可能在PHP標簽的內(nèi)部:
<pre>
? <?php the_excerpt(); ?>
?? <?php the_content(); ?>
?? <?php the_content(); ?>
</pre>
</pre>


Conversely, if your Theme is currently displaying excerpts and you want full posts, replace <tt>the_exerpt</tt> with <tt>the_content</tt>.
Conversely, if your Theme is currently displaying excerpts and you want full posts, replace <tt>the_exerpt</tt> with <tt>the_content</tt>.
與此相反,如果你的主題當前顯示摘錄,但是你想要顯示完整的文章,將<tt>the_exerpt</tt>替換為<tt>the_content</tt>。


=== Display Images Linking to Full Posts ===
=== Display Images Linking to Full Posts ===
=== 顯示圖像鏈接到完整的文章===


Another thing that is interesting to do in a category archive page is to replace the post content with an image that links to your post. To do this, you will first need to put images into the Excerpt sections of your posts. Here's how to do that, for one post:
Another thing that is interesting to do in a category archive page is to replace the post content with an image that links to your post. To do this, you will first need to put images into the Excerpt sections of your posts. Here's how to do that, for one post:
處理類別歸檔頁面的另一個有趣的操作是使用鏈接到你的文章的圖像代替文章內(nèi)容。你首先要將圖像放置到你的文章的摘錄部分。下面是怎樣將圖像放到一篇文章的摘錄部分:
# Upload an image on the post editing screen.
# Upload an image on the post editing screen.
# Switch to the "Code" editor, rather than the "Visual" editor.
# Switch to the "Code" editor, rather than the "Visual" editor.
第95行: 第194行:
# Copy the inserted HTML <tt>img</tt> tag, and paste it into the "Optional Excerpt" section of the post editing screen.
# Copy the inserted HTML <tt>img</tt> tag, and paste it into the "Optional Excerpt" section of the post editing screen.
# Finish writing the post content (you can remove the image), and publish the post.
# Finish writing the post content (you can remove the image), and publish the post.
#將一個圖像上傳到文章編輯界面。
#切換到"Code"編輯器,而不是"Visual"編輯器。
#使用上傳/瀏覽標簽將圖像插入到你的文章。確定插入了完整大小的圖像,沒有鏈接。
#復(fù)制已插入的HTML<tt>img</tt>標簽,并且將標簽粘貼到文章編輯界面的"可選擇的摘錄" 部分。
#結(jié)束編寫文章內(nèi)容(你可以移除圖像),并且發(fā)表文章。


Now you will need to modify your template. We'll use a trick: the [[WordPress:Template Tags/the_excerpt_rss|the_excerpt_rss()]] Template Tag does not put a paragraph tag around the excerpt. So we can use it to insert the <tt>img</tt> HTML and put it inside a link. Here's what you need to put into your Template, in place of using <tt>the_content</tt>:
Now you will need to modify your template. We'll use a trick: the [[WordPress:Template Tags/the_excerpt_rss|the_excerpt_rss()]] Template Tag does not put a paragraph tag around the excerpt. So we can use it to insert the <tt>img</tt> HTML and put it inside a link. Here's what you need to put into your Template, in place of using <tt>the_content</tt>:
現(xiàn)在你需要更改你的模板。我們會使用一種方法:[[WordPress:Template Tags/the_excerpt_rss|the_excerpt_rss()]]。模板標簽沒有將段落標簽放置在摘錄周圍。因為我們使用模板標簽插入<tt>img</tt> HTML并且將其放入鏈接內(nèi)容。下面是我們需要放入模板中,取代<tt>the_content</tt>的內(nèi)容:
<pre>
<a href="<?php the_permalink() ?>">
<?php the_excerpt_rss(); ?>
</a>
</pre>
<pre>
<pre>
<a href="<?php the_permalink() ?>">
<a href="<?php the_permalink() ?>">
第104行: 第218行:


Caveat: using the excerpt this way '''may''' effect your RSS feed, because it places an img tag in the excerpt, instead of text. So if you are going to do this, you probably want to set your options so that the full posts are put in RSS feeds, rather than excerpts.
Caveat: using the excerpt this way '''may''' effect your RSS feed, because it places an img tag in the excerpt, instead of text. So if you are going to do this, you probably want to set your options so that the full posts are put in RSS feeds, rather than excerpts.
告誡:以這種方式處理摘錄'''可能'''影響你的RSS feed,因為將img標簽放入摘錄而不是文本中。因此,如果你準備執(zhí)行這個操作,你可能想要設(shè)置你的選項,這樣完整的文章而不是摘錄就會放入RSS feeds。


=== What categories do you show to the visitors? ===
=== What categories do you show to the visitors? ===
===你想要向訪客顯示什么類別? ===


You can limit the categories in archive and other pages with this code:
You can limit the categories in archive and other pages with this code:
你可以使用這個代碼在歸檔和其它頁面中限制類別:
<pre>
<pre>
<?php query_posts('cat=1&showposts='.get_option('posts_per_page')); ?>
<?php query_posts('cat=1&showposts='.get_option('posts_per_page')); ?>
</pre>
</pre>
<pre>
<?php query_posts('cat=1&showposts='.get_option('posts_per_page')); ?>
</pre>
This is placed before the Loop.
This is placed before the Loop.
這個代碼放置在Loop之前。
You find more info about the query_posts() function parameters here: http://codex.wordpress.org/User:JamesVL/query_posts .
You find more info about the query_posts() function parameters here: http://codex.wordpress.org/User:JamesVL/query_posts .
你可以在這個網(wǎng)站找到更多的有關(guān)query_posts()函數(shù)參數(shù)的信息:http://codex.wordpress.org/User:JamesVL/query_posts 。
The <tt>get_option('posts_per_page')</tt> part of the code uses your blog's options to show a limited number of posts.
The <tt>get_option('posts_per_page')</tt> part of the code uses your blog's options to show a limited number of posts.
代碼的<tt>get_option('posts_per_page')</tt>部分使用你的博客選項顯示有限數(shù)目的文章。


This code is very useful if you would like to separate? the categories. With this code you can make multiple archives. For example:
This code is very useful if you would like to separate? the categories. With this code you can make multiple archives. For example:
如果你想要分開類別,這個代碼非常有用。使用這個代碼,你可以創(chuàng)建多個歸檔。例如:
<pre>
<?php if (have_posts()) : ?>
<?php if (is_month()) {query_posts('year='.get_the_time('Y').'&monthnum='.get_the_time('m').'&author_name=admin&cat=1&showposts='.get_option('posts_per_page')); ?>
? <!-- Do stuff... ->
<?php } ?>
<?php while (have_posts()) : the_post(); ?>
? <!-- post's contents -->
<?php endwhile; ?>
<?php endif; ?>
</pre>
<pre>
<pre>
<?php if (have_posts()) : ?>
<?php if (have_posts()) : ?>
第126行: 第275行:
<?php endif; ?>
<?php endif; ?>
</pre>
</pre>
If you use this code in <tt>archive.php</tt> and navigate to <tt>http://yourblog.com/2008/02/</tt>, show you the <tt>admin</tt> user's post that is posted on 2008.02. in the "first" category.
If you use this code in <tt>archive.php</tt> and navigate to <tt>http://yourblog.com/2008/02/</tt>, show you the <tt>admin</tt> user's post that is posted on 2008.02. in the "first" category.
如果你在<tt>archive.php</tt>中使用這個代碼并且導(dǎo)航到<tt>http://yourblog.com/2008/02/</tt>,向你顯示<tt>admin</tt>用戶的文章,在2008.02發(fā)表,在"第一個"類別中。


= Related =
= Related =
第133行: 第285行:


{{Query String Tag Footer}}
{{Query String Tag Footer}}
= 相關(guān)的 =
{{標簽類別標簽}}
{{查詢字符串標簽頁底文字}}


= Further Reading =
= Further Reading =
= 深入閱讀 =


* [http://wordpress.org/support/topic/38046 Support Forum discussion of Category-based theme] - how to make a Theme that organizes posts by category instead of by date on the home page
* [http://wordpress.org/support/topic/38046 Support Forum discussion of Category-based theme] - how to make a Theme that organizes posts by category instead of by date on the home page
* [[WordPress:Templates]] - Comprehensive list of resources related to Themes and Templates
* [[WordPress:Templates]] - Comprehensive list of resources related to Themes and Templates
* [http://wordpress.org/support/topic/38046 支持論壇上以類別為基礎(chǔ)的主題的討論] – 怎樣使得主題在主頁上按照類別而不是安裝日期來組織文章
* [[WordPress:Templates|模板]] – 與主題和模板相關(guān)的資源的全面列表

2008年9月18日 (四) 11:33的最新版本

Introduction[ ]

介紹[ ]

Since the advent of Themes in WordPress 1.5, changing the look and feel of your WordPress site has become fairly straightforward. For instance, when a viewer clicks on a link to one of the Categories on your site, he or she is taken to a page listing the Posts from that particular Category in chronological order, from newest Posts at the top to oldest at the bottom. There are many display choices, including whether to display the complete post or post excerpts, and what additional information to display (title, author, publish date, last modified time, etc.). Each theme makes different choices, and you might want to change them.

自從WordPress1.5出現(xiàn)了主題,更改你的WordPress站點的外觀和整體效果變得輕而易舉。例如,當訪客點擊你的站點上其中的一個類別鏈接的時候,他或者她就會來到一個頁面,這個頁面以時間順序列出了那個類別的所有文章,將最新發(fā)表的文章列在頂上方,將最遲發(fā)表的文章列在最下方??梢砸栽S多不同的方式顯示文章,包括是否顯示完整的文章還是顯示摘錄,顯示哪些額外信息(標題,作者,發(fā)表日期,最后更改的時間,等等)。每個主題會有許多不同的選擇,你可能想要更改這些選項。

This article explains how to change what happens when the blog viewer is visiting one of your site's Category pages. This involves the use of Themes and Template files, so if you are new to template files, you might want to read WordPress:Using Themes and WordPress:Stepping Into Templates first.

這篇文章解釋了,當博客訪客瀏覽你的站點的其中一個類別頁面的時候,怎樣更改出現(xiàn)的變化。這包括使用主題和主題文件,因此,如果你剛剛接觸模板文件,你可能想要先閱讀使用主題走進模板

What Template File is Used?[ ]

使用什么模板文件?[ ]

The first step in modifying what happens when someone visits a Category page is to figure out which of your theme's files is going to be used to display the posts. This is known as the WordPress:Template Hierarchy.

當有人瀏覽類別頁面的時候,更改發(fā)生變化的第一步是要了解,將要使用你的哪個主題文件來顯示文章。這也稱為模板層級。

In the case of categories, the hierarchy is fairly simple. For instance, suppose the ID number of the Category in question is 6. The Template Hierarchy specifies that WordPress will use the first Template file it finds in your current Theme's directory from the following list:

  1. category-6.php
  2. category.php
  3. archive.php
  4. index.php

關(guān)于類別,層級非常簡單。例如,假如討論的類別ID的數(shù)字是6。模板層級規(guī)定WordPress將會使用WordPress在下面的列表,你的當前主題目錄中找到的第一個模板文件:

  1. category-6.php
  2. category.php
  3. archive.php
  4. index.php

That is, if you do not have a category-6.php, WordPress will check for a category.php, and so on.

也就是說,如果你沒有category-6.php,WordPress就會查看category.php,等等。

So, if you want to make the Category whose ID number is 6 look different from what it is currently (and different from other Category pages), you would want to create a category-6.php file. If you want to make all Category pages look different from other archive pages (such as date and author archives), then you would want to create or modify the category.php file. If you want to make changes to the look of all archive pages, you can create or modify the archive.php file. And if you modify the index.php file, you will affect your entire blog.

因此,如果你希望類別ID是6的類別看起來與當前類別頁面不同(也與其它類別頁面不同),你可能想要創(chuàng)建category-6.php文件。如果你希望所有的類別頁面與其它歸檔頁面看起來不同(例如日期和作者歸檔),那么你可能想要創(chuàng)建或者更改category.php file。如果你想要更改所有的歸檔頁面的外觀,你可以創(chuàng)建或者更改archive.php文件。而且如果你更改了index.php文件,你就會影響你的整個博客。

If you need to create a new file, it is a good idea to copy it from the next file in the hierarchy that exists. For instance, if you want a special display for Category 6, begin by copying the category.php file, or if you don't have one, use archive.php, and so on.

如果你需要創(chuàng)建新的文件,最后從存在的層級中的另一個文件中復(fù)制這個文件。例如,如果你想要特別地顯示類別6,首先復(fù)制category.php文件,或者如果你沒有這個文件,請使用archive.php,等等。

Examples[ ]

例子[ ]

Now that you've figured out which template file in your theme's directory you need to modify, in order to make changes to the look of Category pages, let's look at some examples. In these examples, when it says "edit your template file", it means to edit the file you chose in the section above.

既然現(xiàn)在你已經(jīng)了解需要更改你的主題目錄中的哪個模板文件,為了更改類別頁面的外觀,讓我們看看一些例子。在這些例子中,當顯示"編輯你的模板文件"的時候,意思是編輯你從上面的部分中選擇的文件。

Adding Text to Category Pages[ ]

將文本添加到類別頁面[ ]

Static Text Above Posts[ ]

文章上方的靜態(tài)文本[ ]

Suppose you want some static text displayed before the list of Posts on your Category page(s). By "static", we mean text that remains the same, no matter which posts will be displayed below, and no matter which category is being displayed. Here is how to do it: above WordPress:The Loop section of your Template file, insert the following code:

假如你希望你的類別頁面的文章列表之前顯示一些靜態(tài)文本。"靜態(tài)",指的是,不管下面顯示哪篇文章,文本都保持一致。下面是怎樣顯示靜態(tài)文本的操作:在你的模板文件的The Loop部分的上面,插入以下的代碼:

<p>
This is some text that will display at the top of the Category page.
</p>
<p>
這些文本將會顯示在類別頁面的頂上方。
</p>

Different Text on Some Category Pages[ ]

一些類別頁面上的不同的文本[ ]

A slightly more complex possibility is that you want different text to display depending on which category page the visitor is viewing. Then you would add the "default" text to the main category.php file, and create special category-#.php files (with their own version of the text, as described in the Introduction) for each category that needs special text at the top.

稍微復(fù)雜的情況是,根據(jù)訪客瀏覽不同的類別頁面,你希望顯示不同的文本。那么你將要將"默認"文本添加到主要的category.php文件,并且為需要在頂上方出現(xiàn)特別文本的每個類別,創(chuàng)建特別的category-#.php文件(帶有自己版本的文本,如介紹中所描述的)。

This does however create a lot of files in your theme directory, and can be avoided using the following code OUTSIDE the loop:

這樣,在你的主題目錄中創(chuàng)建了許多文件,而且可以避免在loop 使用下面的代碼:

<?php if (is_category('Category A')) { ?>
<p>This is the text to describe category A</p>
<?php } elseif (is_category('Category B')) { ?>
<p>This is the text to describe category B</p>
<?php } else { ?>
<p>This is some generic text to describe all other category pages, 
I could be left blank</p>
<?php } ?>


<?php if (is_category('Category A')) { ?>
<p>這是描述類別A的文本</p>
<?php } elseif (is_category('Category B')) { ?>
<p>這是描述類別B的文本</p>
<?php } else { ?>
<p>這是描述所有其它類別頁面的一般文本,我可以保留為空白
</p>
<?php } ?>

This does the following. Checks to see if we are looking at Category A, if we are then show the first bit of text, but if we're not then check if we are looking at Category B. If we are then show that bit of text, and finally, if it is neither Category A or B, then show this default text.

這個代碼執(zhí)行以下的操作。查看我們是否在查找類別A,如果我們是在查找類別A,我們會顯示一些文本,如果我們不是在查找類別A,代碼就會查看我們是否在查找類別B。如果我們那時顯示那點文本,而且最后,如果既不是類別A也不是類別B,那么就顯示這個默認文本。

Text Displaying Only on First Page of Archive[ ]

只在歸檔的第一個頁面上顯示文本[ ]

Another thing that can happen is that if your Category contains more posts than will fit on one page (according to the Options for Blog Reading you have set in the Administration panels of your blog), the category archive will split into multiple pages. And maybe you only want to display your static text if the viewer is on the first page of results, or you want to display different text for the other pages.

另一種情況是,如果你的類別包含的文章數(shù)目超過了一個頁面上可以包含的文章數(shù)目(根據(jù)你在你的博客的管理面板上設(shè)置的博客閱讀選項),類別歸檔將會分為幾個頁面。如果訪客在第一個頁面上,你可能只想要顯示你的靜態(tài)文本,或者你想要為其它的頁面顯示不同的文本。

To make this happen, you can use a PHP if statement, which looks at the value of the $paged WordPress variable ($paged is equal to the page number: 1 for the first page of results, 2 for the second page, etc.). It may sound complicated, but it's actually not too bad. Just put the following above WordPress:The Loop:

使得這種情況發(fā)生,你可以使用PHPif聲明,查看$pagedWordPress變數(shù)的參數(shù)值($paged與頁面數(shù)字相等:1代表結(jié)果的第一個頁面,2代表第二個頁面,等等)。聽起來復(fù)雜,但是事實上并不太難。只要將下面的代碼放到The Loop的上面:

<?php if ( $paged < 2 ) { ?>
<p>Text for first page of Category archive.</p>
<?php } else { ?>
<p>Text for subsequent pages of Category.
Can be left out.</p>
<?php } ?>


<?php if ( $paged < 2 ) { ?>
<p>類別歸檔的第一個頁面的文本。</p>
<?php }其它的{ ?>
<p>類別的其它頁面的文本??梢粤魹榭瞻?。</p>
<?php } ?>

Category Name[ ]

類別名[ ]

Another possibility is to put the category name at the top of the page. If this is not already part of your template, you can add it by doing something like this, above WordPress:The Loop:

<p>Category: <?php single_cat_title(); ?></p>

另一種情況是將類別名放到頁面的頂上方。如果這還不是你的模板的一部分,你可以在The Loop上執(zhí)行這個操作,將這個部分添加到你的模板上:

<p>類別: <?php single_cat_title(); ?></p>

Modifying How Posts are Displayed[ ]

更改文章的顯示方式[ ]

Excerpts vs. Full Posts[ ]

摘錄 vs. 完整的文章[ ]

Perhaps you are looking to cut down on the size of your Category pages. You could do this by displaying excerpts rather than the entire content of each Post. To do this, you will just need to find where it says the_content() inside WordPress:The Loop in your Template, and replace it with the_excerpt(). These will most likely be inside PHP tags:

  <?php the_excerpt(); ?>
and
  <?php the_content(); ?>

也許你想要更改你的類別頁面的大小。顯示摘錄而不是每篇文章的整個內(nèi)容,你就可以做到這一點。你只要在你的模板的The Loop內(nèi)找到顯示為the_content()的位置,并將其更改為the_excerpt(),即可。這些很有可能在PHP標簽的內(nèi)部:

  <?php the_excerpt(); ?>
和
  <?php the_content(); ?>

Conversely, if your Theme is currently displaying excerpts and you want full posts, replace the_exerpt with the_content.

與此相反,如果你的主題當前顯示摘錄,但是你想要顯示完整的文章,將the_exerpt替換為the_content。

Display Images Linking to Full Posts[ ]

顯示圖像鏈接到完整的文章[ ]

Another thing that is interesting to do in a category archive page is to replace the post content with an image that links to your post. To do this, you will first need to put images into the Excerpt sections of your posts. Here's how to do that, for one post:

處理類別歸檔頁面的另一個有趣的操作是使用鏈接到你的文章的圖像代替文章內(nèi)容。你首先要將圖像放置到你的文章的摘錄部分。下面是怎樣將圖像放到一篇文章的摘錄部分:

  1. Upload an image on the post editing screen.
  2. Switch to the "Code" editor, rather than the "Visual" editor.
  3. Use the Uploads / Browse tab to insert the image into your post. Make sure to insert the full-sized image, with no link.
  4. Copy the inserted HTML img tag, and paste it into the "Optional Excerpt" section of the post editing screen.
  5. Finish writing the post content (you can remove the image), and publish the post.
  1. 將一個圖像上傳到文章編輯界面。
  2. 切換到"Code"編輯器,而不是"Visual"編輯器。
  3. 使用上傳/瀏覽標簽將圖像插入到你的文章。確定插入了完整大小的圖像,沒有鏈接。
  4. 復(fù)制已插入的HTMLimg標簽,并且將標簽粘貼到文章編輯界面的"可選擇的摘錄" 部分。
  5. 結(jié)束編寫文章內(nèi)容(你可以移除圖像),并且發(fā)表文章。

Now you will need to modify your template. We'll use a trick: the the_excerpt_rss() Template Tag does not put a paragraph tag around the excerpt. So we can use it to insert the img HTML and put it inside a link. Here's what you need to put into your Template, in place of using the_content:

現(xiàn)在你需要更改你的模板。我們會使用一種方法:the_excerpt_rss()。模板標簽沒有將段落標簽放置在摘錄周圍。因為我們使用模板標簽插入img HTML并且將其放入鏈接內(nèi)容。下面是我們需要放入模板中,取代the_content的內(nèi)容:

<a href="<?php the_permalink() ?>">
<?php the_excerpt_rss(); ?>
</a>
<a href="<?php the_permalink() ?>">
<?php the_excerpt_rss(); ?>
</a>

Caveat: using the excerpt this way may effect your RSS feed, because it places an img tag in the excerpt, instead of text. So if you are going to do this, you probably want to set your options so that the full posts are put in RSS feeds, rather than excerpts.

告誡:以這種方式處理摘錄可能影響你的RSS feed,因為將img標簽放入摘錄而不是文本中。因此,如果你準備執(zhí)行這個操作,你可能想要設(shè)置你的選項,這樣完整的文章而不是摘錄就會放入RSS feeds。

What categories do you show to the visitors?[ ]

你想要向訪客顯示什么類別?[ ]

You can limit the categories in archive and other pages with this code:

你可以使用這個代碼在歸檔和其它頁面中限制類別:

<?php query_posts('cat=1&showposts='.get_option('posts_per_page')); ?>


<?php query_posts('cat=1&showposts='.get_option('posts_per_page')); ?>

This is placed before the Loop. 這個代碼放置在Loop之前。

You find more info about the query_posts() function parameters here: http://codex.wordpress.org/User:JamesVL/query_posts .

你可以在這個網(wǎng)站找到更多的有關(guān)query_posts()函數(shù)參數(shù)的信息:http://codex.wordpress.org/User:JamesVL/query_posts 。

The get_option('posts_per_page') part of the code uses your blog's options to show a limited number of posts.

代碼的get_option('posts_per_page')部分使用你的博客選項顯示有限數(shù)目的文章。

This code is very useful if you would like to separate the categories. With this code you can make multiple archives. For example:

如果你想要分開類別,這個代碼非常有用。使用這個代碼,你可以創(chuàng)建多個歸檔。例如:

<?php if (have_posts()) : ?>
 <?php if (is_month()) {query_posts('year='.get_the_time('Y').'&monthnum='.get_the_time('m').'&author_name=admin&cat=1&showposts='.get_option('posts_per_page')); ?>
  <!-- Do stuff... ->
 <?php } ?>
 <?php while (have_posts()) : the_post(); ?>
  <!-- post's contents -->
 <?php endwhile; ?>
<?php endif; ?>


<?php if (have_posts()) : ?>
 <?php if (is_month()) {query_posts('year='.get_the_time('Y').'&monthnum='.get_the_time('m').'&author_name=admin&cat=1&showposts='.get_option('posts_per_page')); ?>
  <!-- Do stuff... ->
 <?php } ?>
 <?php while (have_posts()) : the_post(); ?>
  <!-- post's contents -->
 <?php endwhile; ?>
<?php endif; ?>

If you use this code in archive.php and navigate to http://yourblog.com/2008/02/, show you the admin user's post that is posted on 2008.02. in the "first" category.

如果你在archive.php中使用這個代碼并且導(dǎo)航到

Related[ ]

the_category, the_category_rss, single_cat_title, category_description, wp_dropdown_categories, wp_list_categories, in_category, get_category_parents, get_the_category get_category_link,

模板:Query String Tag Footer

相關(guān)的[ ]

模板:標簽類別標簽

模板:查詢字符串標簽頁底文字

Further Reading[ ]

深入閱讀[ ]