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.
Works for any post type, except attachments and pages.
You can limit the check to a specific post in the first parameter $post.
Use is_singular( $post_type ), when you want to check a post page by its type: post, page, 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)
- 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'); 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(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
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.1.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 ); }