in_category

現在の記事(または指定した任意の記事)が指定されたカテゴリーに割り当てられている場合にテストします。

in_category()は、カテゴリーを割り当てられている親ではない(しかし、記事が子孫のカテゴリーより下の場合はテストしている事を見てください)、カテゴリーの記事が直接(書き込み/編集記事パネルにカテゴリーがチェックされている)割り当てられている場合のみ考慮されます。

このタグは、(バージョン2.7以来)シングル記事の要求のループ内の外あるいは、ループ内の現在の記事テストするために使用する事ができます。あなたがテストしたい記事を指定する場合、どこでも使用する事ができます。

Sponsored Link

原文(翻訳元)Tests if the current post (or any specified post) is assigned to any of the specified categories.

in_category() considers only the categories a post is directly assigned to (the checked categories in Write/Edit Post panel), not the parents of the assigned categories (but see Testing if a post is in a descendant category below).

This tag can be used to test the current post within The Loop or (since Version 2.7) outside the Loop during a single post request. You can use it anywhere if you specify which post you want to test.

in_categoryのファンクションタグ使用方法

<?php in_category( $category, $_post ) ?>

in_categoryのパラメーター

$category

(mixed) (required) 1つまたは、複数のカテゴリが、ID(整数)、名前、スラッグ(文字列)、それらの配列によって指定します。

原文(翻訳元)(mixed) (required) One or more categories specified by ID (integer), name or slug (string), or an array of these

Default: None

$_post

(mixed) (optional) 記事(整数のIDあるいはオブジェクト)。メインのクエリー内の記事あるいは、ループ内の現在の記事がデフォルトです。

原文(翻訳元)(mixed) (optional) The post (integer ID or object). Defaults to the current post in the Loop or the post in the main query.

Default: None

戻り値

記事が任意の指定されたカテゴリーに割り当てられているかどうか。

原文(翻訳元)(boolean)
Whether the post is assigned to any of the specified categories.

注釈

  • Since Version 2.5, you can specify categories by name.
  • Since Version 2.7, you can specify categories by slug.
  • Since Version 2.7, you can check against several categories.
  • Since Version 2.7, you can use this function outside the WordPress Loop (during a single post query, presumably)
  • Since Version 2.7, you can specify any post to test (not just the current one).

ループ内の現在の記事で試す方法

in_category() は、頻繁に現在の記事のカテゴリーに応じてループ内の異なるアクションを取得するために使われます。

原文(翻訳元)in_category() is often used to take different actions within the Loop depending on the current post’s category, e.g.

<?php 
if ( in_category( 'pachoderms' )) {
	// They have long trunks...
} elseif ( in_category( array( 'Tropical Birds', 'small-mammals' ) )) {
	// They are warm-blooded...
} else {
	// &amp; c.
}
?>

ループ外の現在の記事をテストする方法

個々の記事(通常、single.phpテンプレートによって処理される)に要求する間、記事のカテゴリーがループ前であっても開始されるテストをする事ができます。

そのようにテンプレートを切り替えるためにこれを使用する事ができます。

原文(翻訳元)During a request for an individual post (usually handled by the single.php template), you can test that post’s categories even before the Loop is begun.

You could use this to switch templates like so:

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

原文(翻訳元)(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.)

Testing if a post is in a descendant category

When showing a category archive, or showing a category’s posts via query_posts() or get_posts(), WordPress retrieves posts from the specified category and any descendant (child) categories, but in_category() tests only against a post’s assigned categories, not ancestors (parents) of those categories.

For example, if you have a post assigned to the subcategory Fruit → Bananas and not the category Fruit, the Fruit category archive will show the “Bananas” post, but calling in_category(‘fruit’) for that post always returns false.

You can list both the ancestor category and every possible descendant category, e.g.,

<?php if ( in_category( array( 'fruits', 'apples', 'bananas', 'cantaloupes', 'guavas', /*etc*/ ) )) {
	// These are all fruits
}
?>

原文(翻訳元)but you’d have to edit the code every time you moved or added any of the “Fruit” categories.

A more-flexible method is to use or adapt the post_is_in_descendant_category function defined below (you need to copy the function definition below into a template, plugin, or theme functions file before calling it). You can use it together with in_category() like this (in this example 11 is the “Fruit” category’s ID number):

 // 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…
}
?>

原文(翻訳元)If you’d rather refer to the category by name you can use, e.g.,

 post_is_in_descendant_category( get_term_by( 'name', 'fruit', 'category' ) )

原文(翻訳元)
post_is_in_descendant_category function

<?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 &amp;&amp; in_category( $descendants, $_post ) )
			return true;
	}
	return false;
}
?>

関連ファンクションタグ

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

Sponsored Link