WordPress at a glance

is_single() WP 1.5.0

Checks whether the post page of any post type except attachment and page is displayed. Conditional tag.

Works for any post type, except attachments and pages.

You can limit the check to a specific post in the first parameter $post.

If you want to check whether a post of any type, including attachment and page, is displayed in the one check, use is_singular().

You can specify a check for posts of a specific type in is_singular( $post_type ).

  • See: is_page()
  • See: is_singular()
  • Global. WP_Query. $wp_query Global WP_Query instance.
Works based on: WP_Query::is_single()
✈ 1 time = 0.00003s = very fast | 50000 times = 0.02s = speed of light

No Hooks.

Return

true/false. Whether the query is for an existing single post.

Usage

if( is_single( $post ) ){
	// ... 
}
$post(int/string/array)
Post ID, title, slug, or array of such.
Default: ''

Examples

#1 Various usage examples

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
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 in version 2.5.

#2 Enqueue a CSS stylesheet for any single Post page

if( is_single() ){
	wp_enqueue_style( 'handle', 'path/to/cssfile.css' );
}

#3 A note about the Cyrillic and name (slug)

If your website does not change the Cyrillic to the Latin alphabet - plugin Сry to Lat (or similar) is not installed, when a post is created its name changes and Cyrillic transforms to special symbols (ex: contacts will be changed to %d0%ba%d0%be%d0%bd%d1%82%d0%b0%d0%ba%d1%82%d1%8b), so when checking you need to take this into account. I.e. if the name (post_name) is checked instead of the title, do so:

is_single('моя-страница'); // wrong

is_single( sanitize_title('моя-страница') ); // correct

Code of is_single: wp-includes/query.php VER 4.9.8

<?php
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.0' );
		return false;
	}

	return $wp_query->is_single( $post );
}

Related Functions

From tag: Conditional tags (page type and request)

More from tag: Conditional tags (all)

More from category: Queries

More from Template Tags: Main Functions

No comments
    Hello, !     Log In . Register