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 )
.
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
Notes
- See: is_page()
- See: is_singular()
- Global. WP_Query. $wp_query WordPress Query object.
Changelog
Since 1.5.0 | Introduced. |
Code of is_single() is single WP 5.6
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)
- is_404()
- is_admin()
- is_archive()
- is_attachment()
- is_author()
- is_blog_admin()
- is_category()
- is_comment_feed()
- is_customize_preview()
- is_date()
- is_day()
- is_embed()
More from tag: Conditional tags (all)
- cat_is_ancestor_of()
- comments_open()
- email_exists()
- has_category()
- has_custom_header()
- has_excerpt()
- has_nav_menu()
- has_post_thumbnail()
- has_shortcode()
- has_tag()
- has_term()
- have_comments()
- have_posts()
- in_category()
- in_the_loop()
- is_active_sidebar()
More from category: Queries
More from Template Tags: Main Functions
- bloginfo()
- calendar_week_mod()
- get_bloginfo()
- get_calendar()
- get_current_blog_id()
- get_footer()
- get_header()
- get_search_form()
- get_sidebar()