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() 用法



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

默认值: None

is_single() 参数



True on success, false on failure.

is_single() 返回值


/* ----------------------------------
 * wordpress之魂 ©
 * ---------------------------------- */
// When any single Post page is being displayed.

// When Post 17 (ID) is being displayed.

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

// 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之魂 ©
 * ---------------------------------- */
 * 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() 源文件