is_singular()
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() + custom post types.
You can specify the specific type of post that you want to check in the $post_types parameter.
The logic of this function differs from is_single() in that it checks the individual post, while here it checks the type of post.
Use is_single() when you want to check a page by a specific ID, post name, but not by its type: post, page, attachment.
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 Check if this is regular post
if ( is_singular( 'post' ) ){ // viewing a regular post }
#2 Different checks example
// return true if any of the following: is_single(), is_page() or is_attachment(). is_singular(); // True when viewing a post of the Custom Post Type book. is_singular( 'book' ); // True when viewing a post of the Custom Post Type newspaper or book. is_singular( [ 'newspaper', 'book' ] );
#3 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 }
#4 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 }
#5 Determine multiple post types
An example showing how to pass several types of posts in an array.
if( is_singular( [ '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. |
is_singular() is singular code WP 6.7.1
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 ); }