• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

関数リファレンス/in category

提供: WordPress Codex 日本語版
< 関数リファレンス
2007年7月20日 (金) 22:35時点におけるBono (トーク | 投稿記録)による版 (en:Template Tags/in_category 07:55, 16 Apr 2007 Dspt 版)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

原文・最新版: WordPress Codex » in_category

Description

Returns true if the current post is in the specified Category. Normally this tag is used within The Loop, but the $post variable must be set when using this tag outside of the loop.

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 if ( in_category('category_id') ): ?>
  // Some category specific PHP/HTML here
<?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>.

<?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>';
}
?>

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>';
}
?>

Use OUTSIDE The Loop

Normally, this tag must be used inside 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.

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.

<?php
 $post = $wp_query->post;
 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.

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:
  • 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.

Related

カテゴリータグ: the_category(), the_category_rss(), single_cat_title(), category_description(), wp_dropdown_categories(), wp_list_categories(), get_the_category(), get_the_category_by_ID(), get_category_by_slug(), get_the_category_list(), get_category_parents(), get_category_link(), is_category(), in_category()


テンプレートタグへの PHP 関数型パラメータの渡し方関数リファレンステンプレートタグ目次もご覧ください。