is_single()判断当前页面类型是否为文章页

目录

描述

This conditional tag checks if a single post of any post type (except attachment and page post types) is being displayed. If the $post parameter is specified, the function will additionally check if the query is for one of the posts specified. To check for all the post types, use the is_singular() function.

is_single() 描述

用法

<?php is_single($post); ?>

is_single() 用法

参数

$post

(mixed) (可选) Post ID, Post Title, Post Slug, or array of posts (WP 2.5+)

默认值: None

is_single() 参数

返回值

(boolean

True on success, false on failure.

is_single() 返回值

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
is_single();
// When any single Post page is being displayed.

is_single('17');
// When Post 17 (ID) is being displayed.

is_single(17);
// When Post 17 (ID) is being displayed. Integer parameter also works
/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
is_single('Irish Stew');
// When the Post with post_title of "Irish Stew" is being displayed.

is_single('beef-stew');
// When the Post with post_name (slug) of "beef-stew" is being displayed.

is_single(array(17,'beef-stew','Irish Stew'));
// Returns true when the single post being displayed is either post ID 17, or the post_name is "beef-stew", or the post_title is "Irish Stew".  Note: the array ability was added at Version 2.5.

is_single() 示例

注意

  • See Also: is_singular()
  • Although is_single() will usually return true for attachments, this behavior should not be relied upon. It is possible for $is_page and $is_attachment to be true at the same time, and in that case $is_single will be false. For this reason, you should use is_attachment() || is_single() if you want to include attachments, or use is_singular() if you want to include pages too. See ticket #14034 for more information.

is_single() 注意

历史

添加于 版本: 1.5.0

is_single() 历史

源文件

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Is the query for an existing single post?
 *
 * Works for any post type, except attachments and pages
 *
 * If the $post parameter is specified, this function will additionally
 * check if the query is for one of the Posts specified.
 *
 * @see is_page()
 * @see is_singular()
 *
 * @since 1.5.0
 *
 * @global WP_Query $wp_query
 *
 * @param mixed $post Post ID, title, slug, or array of such.
 * @return bool
 */
function is_single( $post = '' ) {
	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_single( $post );
}

is_single() 源文件

相关