- サイトデザイン工事中です。ご意見をお寄せください。
- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
テンプレートタグ/in category
目次 |
説明
現在の投稿(あるいは指定した任意の投稿) に指定したカテゴリーが割り当てられているか調べます。
in_category() は、投稿に直接割り当てられているカテゴリー(新規投稿/編集でチェックしたカテゴリー)のみを考慮します。そのカテゴリーの親は考慮しません(Testing if a post is in a descendant category も参考にしてください)。
このタグは、The Loop 内で現在の投稿、あるいは (Version 2.7 以降で)単一投稿リクエストの場合 Loop 外でテストします。テストしたい投稿を指定すれば、どこでも使用できます。
使い方
例えば、現在処理中(ページを生成中)の投稿が、あるカテゴリID に属するときにのみ、特定の PHP や HTML を動作させたい場合には、これで 'category_id' を指定します。
<?php if ( in_category( $category, $_post ) ): ?> // ここにカテゴリ特有の PHP/HTML を書く <?php endif; ?>
パラメータ
- $category
- (mixed) (必須) ID (整数)、名前またはスラッグ (文字列)、あるいはそれらの配列で指定されるカテゴリー
- 初期値: なし
- IDは、整数むき出しでも文字列として書いてもどちらでも構いません。
in_category(5)in_category('5')
- $_post
- (mixed) (オプション) 投稿 (整数 ID あるいはオブジェクト)。デフォルトは Loop の現在の投稿あるいは主クエリの投稿。
- 初期値: なし
戻り値
- (boolean)
- 投稿に指定したカテゴリーが割り当てられていれば True、そうでなければ false
注
- Version 2.5 以降では、カテゴリー名でカテゴリーを指定できます。
- Version 2.7 以降では、カテゴリースラッグでカテゴリーを指定できます。
- Version 2.7 以降では、複数のカテゴリーについてチェックできます。
- Version 2.7 以降では、(単一投稿クエリでは) WordPress Loop の外で使用できます。
- Version 2.7 以降では、(現在の投稿だけでなく)任意の投稿を指定してテストできます。
用例
Loop 内で現在の投稿をテストする
in_category() は、Loop 内で、投稿のカテゴリーに基づいて異なる処理を行うときによく用います。例:
<?php
if ( in_category( 'pachoderms' )) {
// They have long trunks...
} elseif ( in_category( array( 'Tropical Birds', 'small-mammals' ) )) {
// They are warm-blooded...
} else {
// & c.
}
?>
Loop 外で現在の投稿をテストする
(通常 single.php テンプレートで扱う)個別投稿のリクエストでは、Loop が始まる前にカテゴリーをテストすることができます。
以下のように、これを用いてテンプレートを変更することができます。
<?php
if ( in_category('fruit') ) {
include 'single-fruit.php';
} elseif ( in_category('vegetables') ) {
include 'single-vegetables.php';
} else {
// Continue with normal Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
// ...
}
?>
(Custom Post Templates Plugin は、個別投稿のテンプレートが作れます。単一の投稿だけでなく、所定のカテゴリーの全投稿に使用するテンプレートを追加する例もあります。デフォルトではコメントアウトされていますが、対応する行をアンコメントすると容易に実装できます。訳注: このプラグインは古いかもしれません。 --Mizuno 2010年3月30日 (火) 10:00 (UTC))
子カテゴリー内の投稿かテストする
カテゴリーアーカイブを表示するとき、あるいは query_posts() や get_posts() でカテゴリーの投稿を表示するとき、WordPress は特定のカテゴリーに加えて子カテゴリーを呼び出します。しかし in_category() は投稿に割り当てられたカテゴリーかどうかをテストし、親カテゴリーを対象としません。
例えば、投稿がサブカテゴリー Fruit → Bananas に割り当てられているがカテゴリー Fruit に割り当てられていない場合、Fruit カテゴリーアーカイブは "Bananas" 投稿を表示しますが、この投稿について in_category('fruit') を呼び出すと、常に false を返します。
親カテゴリーとその子カテゴリーを全てリストすることができます。例:
<?php if ( in_category( array( 'fruits', 'apples', 'bananas', 'cantaloupes', 'guavas', /*etc*/ ) )) {
// These are all fruits
}
?>
しかしながら "Fruit" カテゴリーに移動または追加する度にコードを編集する必要があります。
より柔軟な方法は、下記で定義する post_is_in_descendant_category 関数(呼び出す前にテンプレート、プラグイン、またはテーマ関数にコードをコピーする必要がある)を使用することです。下の例のようにすると in_category() と一緒に使用することができます(この例では 11 は "Fruit" カテゴリーの IDです)。
// Post is assigned to "fruit" category or any descendant of "fruit" category?
<?php if ( in_category( 'fruit' ) || post_is_in_descendant_category( 11 ) ) {
// These are all fruits…
}
?>
カテゴリー名で参照したい場合には、以下の例のようにします。
post_is_in_descendant_category( get_term_by( 'name', 'fruit', 'category' ) )
post_is_in_descendant_category 関数
<?php
/**
* Tests if any of a post's assigned categories are descendants of target categories
*
* @param int|array $cats The target categories. Integer ID or array of integer IDs
* @param int|object $_post The post. Omit to test the current post in the Loop or main query
* @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
* @see get_term_by() You can get a category by name or slug, then pass ID to this function
* @uses get_term_children() Passes $cats
* @uses in_category() Passes $_post (can be empty)
* @version 2.7
* @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
*/
function post_is_in_descendant_category( $cats, $_post = null )
{
foreach ( (array) $cats as $cat ) {
// get_term_children() accepts integer ID only
$descendants = get_term_children( (int) $cat, 'category');
if ( $descendants && in_category( $descendants, $_post ) )
return true;
}
return false;
}
?>
外部資料
最新英語版: WordPress Codex » Function Reference/in_category (最新版との差分)
関連
the_category, the_category_rss, single_cat_title, category_description, wp_dropdown_categories, wp_list_categories, get_the_category, get_category_parents, get_category_link, is_category, in_category