YogeshChauhan . com

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

amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

DISTINCT ON: The confusing, unique and useful feature in PostgresPostgresLIMIT and OFFSET in PostgresPostgresIntroduction to components and templates Part 3: Data bindingAngular@forward rule in SCSS (Sass)SCSSAngular: Templates, directives, data binding, Services and dependency injectionAngularHTML Copyright SymbolHTML