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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to convert datetime to date format in JavaScript?JavaScriptWordPress: How to find all posts with a specific custom field value?WordPressHow to update or disable WordPress Post Revisions?WordPressHow to convert a number rounding to a specified number of decimals in JavaScript?JavaScriptWindow innerHeight and innerWidth properties in JavaScriptJavaScriptA Quick Guide to Object-Oriented Programming in PHPPHP