WIKI使用導(dǎo)航
站長百科導(dǎo)航
站長專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機(jī)
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計(jì)算
- 微博營銷
- 虛擬主機(jī)管理系統(tǒng)
- 開放平臺(tái)
- WIKI程序與應(yīng)用
- 美國十大主機(jī)
WordPress:Tag Templates
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站點(diǎn)的外觀和整體效果變得輕而易舉。例如,當(dāng)訪客點(diǎn)擊你的站點(diǎn)上其中的一個(gè)類別鏈接的時(shí)候,他或者她就會(huì)來到一個(gè)頁面,這個(gè)頁面以時(shí)間順序列出了那個(gè)類別的所有文章,將最新發(fā)表的文章列在頂上方,將最遲發(fā)表的文章列在最下方??梢砸栽S多不同的方式顯示文章,包括是否顯示完整的文章還是顯示摘錄,顯示哪些額外信息(標(biāo)題,作者,發(fā)表日期,最后更改的時(shí)間,等等)。每個(gè)主題會(huì)有許多不同的選擇,你可能想要更改這些選項(xiàng)。
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.
這篇文章解釋了,當(dāng)博客訪客瀏覽你的站點(diǎn)的其中一個(gè)類別頁面的時(shí)候,怎樣更改出現(xiàn)的變化。這包括使用主題和主題文件,因此,如果你剛剛接觸模板文件,你可能想要先閱讀使用主題和 走進(jì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.
當(dāng)有人瀏覽類別頁面的時(shí)候,更改發(fā)生變化的第一步是要了解,將要使用你的哪個(gè)主題文件來顯示文章。這也稱為模板層級(jí)。
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:
- category-6.php
- category.php
- archive.php
- index.php
關(guān)于類別,層級(jí)非常簡單。例如,假如討論的類別ID的數(shù)字是6。模板層級(jí)規(guī)定WordPress將會(huì)使用WordPress在下面的列表,你的當(dāng)前主題目錄中找到的第一個(gè)模板文件:
- category-6.php
- category.php
- archive.php
- 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就會(huì)查看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的類別看起來與當(dāng)前類別頁面不同(也與其它類別頁面不同),你可能想要?jiǎng)?chuàng)建category-6.php文件。如果你希望所有的類別頁面與其它歸檔頁面看起來不同(例如日期和作者歸檔),那么你可能想要?jiǎng)?chuàng)建或者更改category.php file。如果你想要更改所有的歸檔頁面的外觀,你可以創(chuàng)建或者更改archive.php文件。而且如果你更改了index.php文件,你就會(huì)影響你的整個(gè)博客。
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.
如果你需要?jiǎng)?chuàng)建新的文件,最后從存在的層級(jí)中的另一個(gè)文件中復(fù)制這個(gè)文件。例如,如果你想要特別地顯示類別6,首先復(fù)制category.php文件,或者如果你沒有這個(gè)文件,請(qǐng)使用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)了解需要更改你的主題目錄中的哪個(gè)模板文件,為了更改類別頁面的外觀,讓我們看看一些例子。在這些例子中,當(dāng)顯示"編輯你的模板文件"的時(shí)候,意思是編輯你從上面的部分中選擇的文件。
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> 這些文本將會(huì)顯示在類別頁面的頂上方。 </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ù)訪客瀏覽不同的類別頁面,你希望顯示不同的文本。那么你將要將"默認(rèn)"文本添加到主要的category.php文件,并且為需要在頂上方出現(xiàn)特別文本的每個(gè)類別,創(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.
這個(gè)代碼執(zhí)行以下的操作。查看我們是否在查找類別A,如果我們是在查找類別A,我們會(huì)顯示一些文本,如果我們不是在查找類別A,代碼就會(huì)查看我們是否在查找類別B。如果我們那時(shí)顯示那點(diǎn)文本,而且最后,如果既不是類別A也不是類別B,那么就顯示這個(gè)默認(rèn)文本。
Text Displaying Only on First Page of Archive[ ]
只在歸檔的第一個(gè)頁面上顯示文本[ ]
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ù)目超過了一個(gè)頁面上可以包含的文章數(shù)目(根據(jù)你在你的博客的管理面板上設(shè)置的博客閱讀選項(xiàng)),類別歸檔將會(huì)分為幾個(gè)頁面。如果訪客在第一個(gè)頁面上,你可能只想要顯示你的靜態(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é)果的第一個(gè)頁面,2代表第二個(gè)頁面,等等)。聽起來復(fù)雜,但是事實(shí)上并不太難。只要將下面的代碼放到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>類別歸檔的第一個(gè)頁面的文本。</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í)行這個(gè)操作,將這個(gè)部分添加到你的模板上:
<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(); ?>
也許你想要更改你的類別頁面的大小。顯示摘錄而不是每篇文章的整個(gè)內(nèi)容,你就可以做到這一點(diǎn)。你只要在你的模板的The Loop內(nèi)找到顯示為the_content()的位置,并將其更改為the_excerpt(),即可。這些很有可能在PHP標(biāo)簽的內(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.
與此相反,如果你的主題當(dāng)前顯示摘錄,但是你想要顯示完整的文章,將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:
處理類別歸檔頁面的另一個(gè)有趣的操作是使用鏈接到你的文章的圖像代替文章內(nèi)容。你首先要將圖像放置到你的文章的摘錄部分。下面是怎樣將圖像放到一篇文章的摘錄部分:
- Upload an image on the post editing screen.
- Switch to the "Code" editor, rather than the "Visual" editor.
- Use the Uploads / Browse tab to insert the image into your post. Make sure to insert the full-sized image, with no link.
- Copy the inserted HTML img 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.
- 將一個(gè)圖像上傳到文章編輯界面。
- 切換到"Code"編輯器,而不是"Visual"編輯器。
- 使用上傳/瀏覽標(biāo)簽將圖像插入到你的文章。確定插入了完整大小的圖像,沒有鏈接。
- 復(fù)制已插入的HTMLimg標(biāo)簽,并且將標(biāo)簽粘貼到文章編輯界面的"可選擇的摘錄" 部分。
- 結(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)在你需要更改你的模板。我們會(huì)使用一種方法:the_excerpt_rss()。模板標(biāo)簽沒有將段落標(biāo)簽放置在摘錄周圍。因?yàn)槲覀兪褂媚0鍢?biāo)簽插入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,因?yàn)閷mg標(biāo)簽放入摘錄而不是文本中。因此,如果你準(zhǔn)備執(zhí)行這個(gè)操作,你可能想要設(shè)置你的選項(xiàng),這樣完整的文章而不是摘錄就會(huì)放入RSS feeds。
What categories do you show to the visitors?[ ]
你想要向訪客顯示什么類別?[ ]
You can limit the categories in archive and other pages with this code:
你可以使用這個(gè)代碼在歸檔和其它頁面中限制類別:
<?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. 這個(gè)代碼放置在Loop之前。
You find more info about the query_posts() function parameters here: http://codex.wordpress.org/User:JamesVL/query_posts .
你可以在這個(gè)網(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')部分使用你的博客選項(xiàng)顯示有限數(shù)目的文章。
This code is very useful if you would like to separate the categories. With this code you can make multiple archives. For example:
如果你想要分開類別,這個(gè)代碼非常有用。使用這個(gè)代碼,你可以創(chuàng)建多個(gè)歸檔。例如:
<?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中使用這個(gè)代碼并且導(dǎo)航到
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,
Related[ ]
相關(guān)的[ ]
Further Reading[ ]
深入閱讀[ ]