WordPress:Template Tags/in category

来自站长百科
Fludlen讨论 | 贡献2008年7月1日 (二) 16:20的版本
跳转至: 导航、​ 搜索

Description

描述

Returns true if the current post is in the specified Category. Normally this tag is used within WordPress:The Loop, but the $post variable must be set when [[WordPress:#Use OUTSIDE The Loop|using this tag outside of the loop]].

如果当前的文章在规定的类别中,返回真。通常这个标签是在The Loop内部使用的,但是当[[WordPress:#Use OUTSIDE The Loop|在 loop外使用这个标签的时候]],必须设置$post变数。

Usage

用法

Suppose you want to execute some specific PHP or HTML only if the current post being processed is in a category with a category ID number we'll represent here as 'category_id'.

假定你想要执行某些特别的PHP或者HTML只有当前正在处理的文章,所属的类别ID,我们在这里表示为'类别_id',才可以。

<?php if ( in_category('category_id') ): ?>
  // Some category specific PHP/HTML here
<?php endif; ?>
<?php if ( in_category('category_id') ): ?>
  // 一些类别特别的PHP/HTML 
<?php endif; ?>

Examples

例子

Display Some Category Specific Text

显示一些类别特别的文本

Display <span class="good-cat-5">This is a nice category</span> in each post which is a member of category 5, otherwise display <span class="bad-cat">This is a BAD category</span>.

显示<span class="good-cat-5">这是每篇文章中的一个非常好的类别</span>,这些文章属于类别5,否则的话,显示<span class="bad-cat">这是一个比较差的类别</span>

<?php if ( in_category(5) ) {
echo '<span class="my-cat-5">This is a nice category</span>';
} else {
echo '<span class="bad-cat">This is a BAD category</span>';
}
?>
<?php if ( in_category(5) ) {
echo '<span class="my-cat-5">这是一个好类别</span>';
} else {
echo '<span class="bad-cat">这是一个差类别</span>';
}
?>

Unfortunately, in_category doesn't understand category child-parent relationships. If, for example, category 11 (bananas) is a child of category 2 (fruits), in_category('2') will return FALSE when viewing post about bananas. So if you want the same text to be applied to the category AND all its sub-categories, you'll have to list them all. Syntax like in_category(2,11) is not allowed. You'll have to use PHP || (logical OR) && (logical AND) in the expression.

<?php if ( in_category(2) || in_category (11) || in_category (12)[more categories abouth other fruits - this can get messy] ) {
echo '<span class="fruits">This is about different kinds of fruits</span>';
} else {
echo '<span class="bad-cat">Not tasty! Not healthy!</span>';
}
?>



不幸地是, in_category 不了解母类别与子类别的关系。如果,例如,类别11(香蕉)是类别2(水果)的子类别,当查看关于香蕉的文章的时候, in_category('2') 会返回错误的。 因此,如果你想要同样的文本适用于母类别及所有的子类别,你就需要列出所有这些子类被。 像 in_category(2,11) 这样的语法是得不到允许的。你需要在表述中使用 PHP || (逻辑词 或) && (逻辑词 与) 。

<?php 如果( in_category(2) || in_category (11) || in_category (12)[更多关于其它水果的类别 – 可能会变得混乱] ) {
echo '<span class="fruits">这关于不同种类的水果</span>';
} 其余的{
echo '<span class="bad-cat">不好吃!不健康!</span>';
}
?>




Use OUTSIDE The Loop

在Loop外使用

Normally, this tag must be used inside WordPress:The Loop because it depends on a WordPress PHP variable ($post) that is assigned a value only when The Loop runs. However, you can manually assign this variable and then use the tag just fine.

一般来说,这个标签必须用在The Loop内部,因为标签使用取决于一个WordPress PHP 变数($post),而只有Loop运行的时候,这个变数才会拥有一个值。然而,你可以动手赋予这个参数一个值,可以在Loop外使用标签。

For example, suppose you want a single.php Template File in your Theme that will display a completely different page depending on what category the individual post is in. Calling in_category() from within The Loop may not be convenient for your Template. So use the following as your Theme's single.php.

例如,假如你希望主题中有个single.php 模板文件,根据单篇文章属于类别的不同,主题会展现一个完全不同的网页。在Loop中调用in_category(),对你的主题可能造成不便。因此使用下面的内容,作为你的主题的single.php

<?php
 if ( have_posts() ) { the_post(); rewind_posts(); }
 if ( in_category(17) ) {
 include(TEMPLATEPATH . '/single2.php');
 } else {
 include(TEMPLATEPATH . '/single1.php');
 }
 ?>


<?php
 if ( have_posts() ) { the_post(); rewind_posts(); }
 if ( in_category(17) ) {
 include(TEMPLATEPATH . '/single2.php');
 } else {
 include(TEMPLATEPATH . '/single1.php');
 }
 ?>

This will use single2.php as the Template if the post is in category 17 and single1.php otherwise. This essentially pulls the first post into the correct variables, then resets the main WordPress query to start from there again when the main Loop does run.

如果文章是在类别17,会使用single2.php作为模板,否则的话,会使用single1.php作为模板。这样第一篇文章被拖到正确的变数上,然后重新设置了WordPress查询,当主要的Loop运行的时候,WordPress查询重新从那里开始,

Parameters

参数

category_id
(integer) The category ID of the category for which you wish to test. The parameter may either be passed as a bare integer or as a string:
类别_id
(整数)你想要测试的类别的类别ID。参数可能会以一个整数或者一个字符串的形式传送。
  • in_category(5)
  • in_category('5')
  • in_category(5)
  • in_category('5')

Plugin Options

插件选项

Eventually, someone will make a clever plugin that will do all of this automatically. At that point this example will become obsolete. However, the Custom Post Templates Plugin allows for creation of templates for single posts. It also shows an example of how to add a template which is used for all posts in a given category, not just a single post. That example is commented out in the plugin by default but can be easily implemented by uncommenting the appropriate lines.

最终,某个人会编写一个功能更强的插件,能够自动地帮你完成所有的这些步骤。那时,这个例子就不会再使用了。然而,自定义文章模板插件允许为单篇文章创建模板。同时也举了一个例子,关于怎样添加一个模板,这个模板用于一个给定的类别中的所有文章,而不是某一篇文章。默认情况下,插件没有运行那个例子,但是只要uncomment适当的那一行,就可以轻易地执行这个例子。

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,

模板:PHP Function Tag Footer

相关的

Description

描述

Displays a link to the category or categories a post belongs to. This tag must be used within WordPress:The Loop.

显示文章所属的类别的连接。必须在The Loop内,使用这个标签。

Usage

用法

%%% <?php the_category('separator', 'parents' ); ?> %%% %%% <?php the_category('separator', 'parents' ); ?> %%%

Examples

Separated by Space

例子

由空格分开

This usage lists categories with a space as the separator.

这个用法用分隔符,隔开类别,列出。

<p>Categories: <?php the_category(' '); ?></p>
<p>Categories: <?php the_category(' '); ?></p>
Categories: [[WordPress:#Examples|WordPress]] [[WordPress:#Examples|Computers]] [[WordPress:#Examples|Blogging]]
类别: [[WordPress:#Examples|WordPress]] [[WordPress:#Examples|电脑]] [[WordPress:#Examples|写博客]]

Separated by Comma

用逗号分开

Displays links to categories, each category separated by a comma (if more than one).

显示类别连接,(如果多于一个类别)类别之间用逗号分开。

<p>This post is in: <?php the_category(', '); ?></p>

<p>This post is in: <?php the_category(', '); ?></p>

This post is in: [[WordPress:#Examples|WordPress]], [[WordPress:#Examples|Computers]], [[WordPress:#Examples|Blogging]]


This post is in: [[WordPress:#Examples|WordPress]], [[WordPress:#Examples|电脑]], [[WordPress:#Examples|写博客]]


Separated by Arrow

用箭头分开

Displays links to categories with an arrow (>) separating the categories. (Note: Take care when using this, since some viewers may interpret a category following a > as a subcategory of the one preceding it.)

用箭头显示类别链接(>)分开类别。(注: 使用这种方法的时候,应该注意,有的访客认为类别后面的> 是前面类别的子类别。)

<p>Categories: <?php the_category(' &gt; '); ?></p>

<p>Categories: <?php the_category(' &gt; '); ?></p>

Categories: [[WordPress:#Examples|WordPress]] > [[WordPress:#Examples|Computers]] > [[WordPress:#Examples|Blogging]]
类别: [[WordPress:#Examples|WordPress]] > [[WordPress:#Examples|电脑]] > [[WordPress:#Examples|写博客]]

Separated by a Bullet

用Bullet分开

Displays links to categories with a bullet (•) separating the categories.

使用bullet (•)分开类别,显示类别链接

%%%

Post Categories: <?php the_category(' &bull; '); ?>

%%% %%%

文章类别: <?php the_category(' &bull; '); ?>

%%%

Post Categories: [[WordPress:#Examples|WordPress]] • [[WordPress:#Examples|Computers]] • [[WordPress:#Examples|Blogging]]
文章类别: [[WordPress:#Examples|WordPress]] • [[WordPress:#Examples|电脑]] • [[WordPress:#Examples|写博客]]

Parameters

参数

separator
(string) Text or character to display between each category link. The default is to place the links in an unordered list.

;分隔符: (string)链接类别之间显示的文本或者字符。默认是将链接放在无序列表上。

parents
(string) How to display links that reside in child (sub) categories. Options are:
parents
(string)怎样显示子类别中的链接。选项有:
  • 'multiple' - Display separate links to parent and child categories, exhibiting "parent/child" relationship.
  • 'multiple' -分开显示母类别和子类别的链接,显示为"母/子"关系。
  • 'single' - Display link to child category only, with link text exhibiting "parent/child" relationship.
  • 'single' -只显示子类别链接,链接文本显示为"母/子"关系。
Note: Default is a link to the child category, with no relationship exhibited.

:注:默认是链接到子类别的一个链接,不显示关系。

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,

模板:PHP Function Tag Footer


相关的

模板:标签类别标签

模板:PHP 函数标签页底文字

模板:PHP 函数 标签 页底文字