is_singular() WP 1.5.0
Checks whether the post page is viewed (post, page, attachment, any post type). Conditional Tag.
This function combines conditional tags: is_single(), is_page(), is_attachment() and custom post types.
You can specify the specific type of post that you want to check in the $post_types parameter.
1 time = 0.000001s = speed of light | 50000 times = 0.01s = speed of light | PHP 7.2.5, WP 5.0.2
No Hooks.
Return
true/false. Whether the query is for an existing single post or any of the given post types.
Usage
if( is_singular($post_types) ){ // ... }
- $post_types(string/array)
- Post type or array of post types.
Default: ''
Examples
#1 Content only on individual pages
With this code, you can display different ads units in the sidebar for posts pages and other pages (archives).
if ( is_singular() ) { // Advertising №1 } else { // Advertising №2 }
#2 How to determine a single page of the specified type
This example shows how to determine a single page of the post type: book
if( is_singular('book') ){ // Code that will only work on pages with the 'book' post type }
#3 Determine multiple post types
An example showing how to pass several types of posts in an array.
if( is_singular( array('newspaper', 'book') ) ){ // some code }
Notes
- See: is_page()
- See: is_single()
- Global. WP_Query. $wp_query WordPress Query object.
Changelog
Since 1.5.0 | Introduced. |
Code of is_singular() is singular WP 5.6
function is_singular( $post_types = '' ) {
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_singular( $post_types );
}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()