YogeshChauhan.com
How to check if the page is single post page in WordPress?
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

dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS Mar 2 WordPress: How to access first and random row values from a repeater field in ACF? Mar 1 The basics of CSS Box model Feb 27 WordPress: How to display slider repeater fields in ACF? Feb 26 Simple Page Hit Counter in PHP

You might also like these

Kanban vs Scrum: The two frameworks of agile principlesMiscWhere is the PHP log file located on Mac OS?PHPHow to Access a Global Variable From Inside a Function in PHP?PHPWhat is the best way to add JavaScript Code into HTML?HTMLHow to add and remove list items using JavaScript?JavaScriptthe box-sizing property in CSSCSS