is_single()
Checks whether a post page (of any post type) is displayed. Posts with attachment
and page
post types will not be checked. Conditional tag.
You can specify a particular post or multiple posts to check if we are on the page of the specified post(s).
Use is_singular( $post_type ), when you need to check a post page by its type: post, page, or attachment, but not by a specific ID, title, slug.
No Hooks.
Return
true|false
. Whether the query is for an existing single post.
Usage
if( is_single( $post ) ){ // ... }
- $post(int/string/array)
The post ID, post title or post slug, in order to identify a specific post.
You can pass an array of any of those values to check several posts.
The post is checked in the following order: ID, post_title, post_name.
Also, for tree-like posts (like page), you can pass the path:
books/my-book
in this case the search will be performed by the get_page_by_path() function.Default: ''
Examples
#1 Various usage examples
is_single(); // When any single Post page is being displayed. is_single( '17' ); is_single( 17 ); // When Post 17 (ID) is being displayed. 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( [ 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. is_single( 'music/flamenko' ); // Will work for a treelike post with the name 'flamenko', child to a post with the name 'music'.
#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
Notes
- See: is_page()
- See: is_singular()
- Global. WP_Query. $wp_query WordPress Query object.
Changelog
Since 1.5.0 | Introduced. |
is_single() is single code WP 6.7.1
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 ); }