WordPress:Category 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版本的主题的产生,更改WordPress站点的外观和整体效果变得非常简单。例如,当一个访客点击链接,链接到你的站点的某个类别的时候,他或者她会来到网页,网页按时间顺序列出了某个类别的所有文章,最新发表的文章在最上面,越早发表的文章越在下面。文章可以以许多种方式显示,包括是否显示整篇文章还是显示文章摘录,显示什么样的额外信息(标题,作者,发表日期,最新更改的时间,等等)。每个主题都可以拥有许多不同的选择,而且你可能想要更改这些选择。
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.
这篇文章解释了,博客访客访问你的站点某个类别的网页的时候,会出现什么情况。这包括使用主题和模板文件,因此,如果你是模板文件新手,你可能想要首先阅读使用主题 和走进模板。
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.
当某人访问类别网页的时候,更改发生什么的第一步是要了解,会使用你的哪个主题的文件,来显示文章。这又称为模板层级。
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:
关于类别方面,层级非常简单。例如,假如正被讨论的类别ID数字为6。模板层级规定WordPress使用,WordPress在下面列表中你的当前主题目录中找到的第一个模板文件:
- category-6.php
- category.php
- archive.php
- index.php
- 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会查找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的类别看起来与当前的类别不同(而且与其它的类别网页也不同),你可能想要创建category-6.php文件。如果你想要使得所有的类别网页看起来与其它的归档网页不同(例如日期和作者归档),那么你可能想要创建或者更改category.php file。如果你想要更改所有的归档网页的外观,你可以创建或者更改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.
如果你想要创建一个新的文件,从存在的模板层级中的下一个文件中复制文件,是个好主意。例如,如果你想要特别显示类别6,首先复制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.
既然现在你已经了解你需要更改主题目录中的哪个模板文件,为了更改类别网页的外观,让我们来看看几个例子。在这些例子中,当显示"编辑你的模板文件"的时候,意思是编辑你在上面的部分中选择的文件。
Adding Text to Category Pages[ ]
给类别网页添加文本[ ]
Static Text Above Posts[ ]
文章上面的静态文本[ ]
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:
假如你希望类别网页上的文本列表之前显示一些静态文本。"静态的",我们指的不管下面显示什么文章,不管正在显示什么类别,文本都是相同的。下面是我们怎样实现这个操作:在模板文件的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.
一个稍微复杂的可能性是,根据访客正在访问哪个类别网页,你希望显示不同的文本。然后,你会向主要的category.php文件添加"默认"文件,并且为需要在顶上方显示特别的文本的类别创建特别的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:
这的确在你的主题目录中创建了许多文件,而且可以避免在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.
这执行以下的操作。查看我们是否正在查找类别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.
另一种可能发生的情况是,如果你的类别包含了太多的的文章,一个网页上放不下(根据你在博客管理面板上设置的博客阅读选项),类别归档就会分为几个网页。而且如果访客在结果的首页上,你可能只想显示静态文本,或者你想要为其它的网页显示不同的文本。
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:
如果你希望上述情况发生,你可以使用一个PHP如果声明,查看$pagedWordPress变数的参数值($paged与网页数字相同:1代表结果的第一个网页,2代表第二个网页,等等)。这听起来很复杂,但是事实上并不太难。只要将下面的内容,放在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 } else { ?> <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:
另一种可能性是将类别名放在网页的顶端。如果这还不是你的模板的一部分,你可以添加,通过在The Loop上执行:
<p>Category: <?php single_cat_title(); ?></p>
<p>Category: <?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:
也许你正想要试图减小你的类别网页的大小。通过显示摘录而不是整篇文章,你就能减小类别网页。要做到这一点,你只要找到你的模板中The Loop内显示the_content()位置,并且将这个更改为the_excerpt()。这些很可能在PHP标签内:
<?php the_excerpt(); ?> and <?php the_content(); ?>
<?php the_excerpt(); ?> and <?php the_content(); ?>
Conversely, if your Theme is currently displaying excerpts and you want full posts, replace the_exerpt with the_content.
与此相反,如果你的主题当前显示摘录而且你想要整篇文章,请用the_content替换the_exerpt。
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:
另一件处理类别归档网页有趣的事项是用链接到你的文章的图像取代文章内容。要做到这一点,首先你需要将图像放入你的文章的摘录部分。下面是怎样操作,关于一篇文章:
- 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.
- 复制插入的HTMLimg标签,并且将标签粘贴到文章编辑界面的"可选择的摘录"部分。
- Finish writing the post content (you can remove the image), and publish the post.
- 完成编写文章内容(你可以移除图像),并且发表文章。
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:
现在你需要更改你的模板。我们会使用一个技巧:the_excerpt_rss()模板标签没有在摘录的周围放上段落标签。因此,我们可以使用它插入imgHTML并且将它放在一个链接的内部。下面是你需要放入模板中的内容,来替换the_content:
<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标签,而不是文本。因此,如果你准备这么执行,你可能想要设置你的选项,这样整篇文章而不是摘录都放入了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 . The get_option('posts_per_page') part of the code uses your blog's options to show a limited number of posts.
你可以在这里找到更多的关于query_posts()函数参数的信息:
http://codex.wordpress.org/User:JamesVL/query_posts。
代码的get_option('posts_per_page')部分使用你的博客选项显示有限数目的文章。
This code is very useful if you would like to separate the categories. With this code you can make multiple archives. For example:
如果你想要分开类别,这个代码非常有用。使用这个代码,你可以创建多个归档。例如:
<?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中使用这个代码并且导航到http://yourblog.com/2008/02/,向你显示管理员用户的文章,发表于2008年2月,在"第一个"类别中。
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,
Further Reading[ ]
深入阅读[ ]
- 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
- 支持论坛讨论以类别为基础的主题 – 怎样制作主题,主题可以通过类别而不是通过主页上的日期来组织为文章
- 模板 – 与主题和模板相关的资源的完整列表