WordPress:Template Tags/in category

来自站长百科
跳转至: 导航、​ 搜索

描述[ ]

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

用法[ ]

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

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

例子[ ]

显示一些类别特别的文本[ ]

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

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


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

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

在Loop外使用[ ]

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

例如,假如你希望主题中有个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');
 }
 ?>

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

参数[ ]

category_id
(integer) 你想要测试的类别的类别ID。参数可能会以一个整数或者一个字符串的形式传送。
  • in_category(5)
  • in_category('5')

插件选项[ ]

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

相关的[ ]

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 函数 标签 页底文字