in_category() WordPress関数

スポンサードリンク

スポンサードリンク

in_category()

参照:WordPress Codex-in_category

構文

in_category ( ①$category, ②$_post[オプション] )

in_category()の詳細

 動 作 

現在の投稿(又は指定した任意の投稿)に、指定したカテゴリーが割り当てられているか調べる。

 

 返り値 

(真偽値)

ture:指定したカテゴリーが投稿に割り当てられていた場合。

false:割り当てられていない場合。

 

 引 数 

①$category

(mixed)(必須)

ID(整数) or

名前(文字列)or

スラッグ(文字列)

 

②$_post[オプション]

(mixed)[初期値:なし]

デフォルトはループ内の現在の投稿、又はメインクエリの投稿。

 

in_category()のコード

参照:tags/5.3/src/wp-includes/category-template.php

<?php

//category-template.phpより

/**
 * Checks if the current post is within any of the given categories.
 *
 * The given categories are checked against the post's categories' term_ids, names and slugs.
 * Categories given as integers will only be checked against the post's categories' term_ids.
 *
 * Prior to v2.5 of WordPress, category names were not supported.
 * Prior to v2.7, category slugs were not supported.
 * Prior to v2.7, only one category could be compared: in_category( $single_category ).
 * Prior to v2.7, this function could only be used in the WordPress Loop.
 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
 *
 * For more information on this and similar theme functions, check out
 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 * Conditional Tags} article in the Theme Developer Handbook.
 *
 * @since 1.2.0
 *
 * @param int|string|array $category Category ID, name or slug, or array of said.
 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
 * @return bool True if the current post is in any of the given categories.
 */
function in_category( $category, $post = null ) {
	if ( empty( $category ) ) {
		return false;
	}

	return has_category( $category, $post );
}

/**
 * Check if the current post has any of given category.
 *
 * @since 3.1.0
 *
 * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for.
 * @param int|object       $post     Optional. Post to check instead of the current post.
 * @return bool True if the current post has any of the given categories (or any category, if no category specified).
 */
function has_category( $category = '', $post = null ) {
	return has_term( $category, 'category', $post );
}

/**
 * Check if the current post has any of given terms.
 *
 * The given terms are checked against the post's terms' term_ids, names and slugs.
 * Terms given as integers will only be checked against the post's terms' term_ids.
 * If no terms are given, determines if post has any terms.
 *
 * @since 3.1.0
 *
 * @param string|int|array $term     Optional. The term name/term_id/slug or array of them to check for.
 * @param string           $taxonomy Taxonomy name.
 * @param int|WP_Post      $post     Optional. Post to check instead of the current post.
 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
 */
function has_term( $term = '', $taxonomy = '', $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$r = is_object_in_term( $post->ID, $taxonomy, $term );
	if ( is_wp_error( $r ) ) {
		return false;
	}

	return $r;
}

 

in_category()を使ったコード例

 実装 

<?php
//以下、すべて同じ動作になる。

in_category(1);

in_category( ' ID1のカテゴリー名 ' );

in_category( ' ID1のslug ' );


?>

 

 

スポンサードリンク

スポンサードリンク