Yogesh Chauhan's Blog

How to check if the page is single post page in WordPress?

in WordPress on February 1, 2021

We can use is_single() Conditional Tag to detect the single post page.

Syntax


is_single( int|string|int[]|string[] $post = '' )

This functions determines whether the query is for an existing single post and checks for any post type, except attachments and pages.

As you can see in the syntax, we can pass the $post parameter and this function will additionally check if the query is for one of the Posts specified.

We can pass post ID, title, slug, or array of such as parameter to this function.

👉 Although is_single() will usually return true for attachments, this behavior should not be relied upon. It is possible for $is_page and $is_attachment to be true at the same time, and in that case $is_single will be false. For this reason, you should use is_attachment() || is_single() if you want to include attachments, or use is_singular() if you want to include pages too.

Examples


is_single();
// When any single Post page is being displayed.
 
is_single('1');
// When Post 1 (ID) is being displayed.
 
is_single(1);
// When Post 1 (ID) is being displayed. 
// Integer parameter also works => is_single('How to check if the page is single post page in WordPress?');
// When the Post with post_title of "How to check if the page is single post page in WordPress?" is being displayed.
 
is_single('how-to-check-if-the-page-is-single-post-page-in-wordpress');
// When the Post with post_name (slug) of "how-to-check-if-the-page-is-single-post-page-in-wordpress" is being displayed.
 
is_single(array(1,'how-to-check-if-the-page-is-single-post-page-in-wordpress','Irish Stew'));
// Returns true when the single post being displayed is either post ID 1,
// or the post_name is "how-to-check-if-the-page-is-single-post-page-in-wordpress", or the post_title is "How to check if the page is single post page in WordPress?".

is_single( array( 17, 19, 1, 11 ) ) 
Returns true when the single post being displayed is either post ID = 17, post ID = 19, post ID = 1 or post ID = 11.

is_single( array( 'slug-1', 'slug-2', 'slug-3' ) ) 
Returns true when the single post being displayed is either the post_name "slug-1", post_name "slug-2" or post_name "slug-3".

is_single( array( 'Title 1', 'Title 2', 'Title 3' ) ) 
Returns true when the single post being displayed is either the post_title is "Title 1", post_title is "Title 2" or post_title is "Title 3".


Credit: WordPress Dev


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to use HTML picture Tag?HTMLHow to force your website to load securely with an .htaccess file?MiscellaneousQuick Introduction to 11 Array Iteration Methods in JavaScriptJavaScriptSome interesting HTML Input Attributes to rememberHTMLHow to detect the Blog Page in WordPress?WordPressWhat is Host Hardening and What are some Important Hardening Steps?Miscellaneous