WordPress:Tag Templates

来自站长百科
Fludlen讨论 | 贡献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出现了主题,更改你的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:

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

关于类别,层级非常简单。例如,假如讨论的类别ID的数字是6。模板层级规定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的类别看起来与当前类别页面不同(也与其它类别页面不同),你可能想要创建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,如果我们是在查找类别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.

另一种情况是,如果你的类别包含的文章数目超过了一个页面上可以包含的文章数目(根据你在你的博客的管理面板上设置的博客阅读选项),类别归档将会分为几个页面。如果访客在第一个页面上,你可能只想要显示你的静态文本,或者你想要为其它的页面显示不同的文本。

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:

使得这种情况发生,你可以使用PHPif声明,查看$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 }其它的{ ?>
<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上执行这个操作,将这个部分添加到你的模板上:

<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(); ?>

也许你想要更改你的类别页面的大小。显示摘录而不是每篇文章的整个内容,你就可以做到这一点。你只要在你的模板的The Loop内找到显示为the_content()的位置,并将其更改为the_excerpt(),即可。这些很有可能在PHP标签的内部:

  <?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:

处理类别归档页面的另一个有趣的操作是使用链接到你的文章的图像代替文章内容。你首先要将图像放置到你的文章的摘录部分。下面是怎样将图像放到一篇文章的摘录部分:

  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. 复制已插入的HTMLimg标签,并且将标签粘贴到文章编辑界面的"可选择的摘录" 部分。
  5. 结束编写文章内容(你可以移除图像),并且发表文章。

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()。模板标签没有将段落标签放置在摘录周围。因为我们使用模板标签插入img HTML并且将其放入链接内容。下面是我们需要放入模板中,取代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 .

你可以在这个网站找到更多的有关query_posts()函数参数的信息: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')部分使用你的博客选项显示有限数目的文章。

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/,向你显示admin用户的文章,在2008.02发表,在"第一个"类别中。

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

相关的

模板:标签类别标签

模板:查询字符串标签页底文字

Further Reading

深入阅读