is_category()判断当前页面是否为分类目录

目录

描述

This Conditional Tag checks if a Category archive page is being displayed. This is a boolean function, meaning it returns either TRUE or FALSE.

To test if a post is in a category use in_category().

is_category() 描述

用法

<?php is_category$category ); ?>

is_category() 用法

参数

$category

(mixed) (可选) Category ID, Category Title, Category Slug or Array of IDs, names, and slugs.

默认值: None

is_category() 参数

返回值

(boolean) 

True on success, false on failure.

is_category() 返回值

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
is_category();
// When any Category archive page is being displayed.

is_category( '9' );
// When the archive page for Category 9 is being displayed.

is_category( 'Stinky Cheeses' );
// When the archive page for the Category with Name "Stinky Cheeses" is being displayed.

is_category( 'blue-cheese' );
// When the archive page for the Category with Category Slug "blue-cheese" is being displayed.

is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) );
// Returns true when the category of posts being displayed is either term_ID 9, or slug "blue-cheese", or name "Stinky Cheeses". Note: the array ability was added at Version 2.5.

is_category() 示例

注意

历史

  • 添加于 版本: 1.5.0

is_category() 历史

源文件

is_category() 函数的代码位于 wp-includes/query.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Is the query for an existing category archive page?
 *
 * If the $category parameter is specified, this function will additionally
 * check if the query is for one of the categories specified.
 *
 * @since 1.5.0
 *
 * @global WP_Query $wp_query
 *
 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
 * @return bool
 */
function is_category( $category = '' ) {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
		return false;
	}

	return $wp_query->is_category( $category );
}

is_category() 源文件

相关