How to Check if the Current Page is a Post in WordPress

5 from 5
from 4 user
(Rate this post)
How to Check if the Current Page is a Post in WordPress

WP developers use the is_single function to detect regular WordPress posts on the current user's browser. For example, to check if the current page is an article (WordPress post), look for the is_single function to return a true value in a PHP condition. This function also can do more! Read this comprehensive article to learn about the is_single function and its usage.

What WordPress Function is Responsible for Checking if the Current Page is a Single Post?

The WordPress is_single function can determine a post’s page in an active visitor’s browser.

Using the is_single function, you can execute blocks of codes only if the current page is a single page (post page) in WordPress.

This function returns a true value if the current page is a post of any type (default and custom types).

Look for a false value if the current user is on a “page” (not a wp post = false value).

Note: is_single() will be true for attachments (usually). detect attachments, use is_attachment() to be sure.

is_single function syntax

This WP function accepts an optional parameter $post.

If you want to check if the current page displays a specific post, determine what post you want to check. You can use an integer value for a specific post ID or an array of Integers for multiple post IDs.

The is_single function also accepts string values for $post to check the post status by post title, slug, or an array containing these values.

Look at the below example to understand what this function is doing.

Check if the current page is a single page using the is_single function

We used the is_single function in three examples of different situations:

Only check if the current user is on a post (just check if it’s a general WP post, ID is not necessary)

is_single() with no argument will detect posts in general.

if(is_single()){
	echo "This is a Post of any type!";
}
else{
	echo "This is a NOT a Post!";
}

Check if the current user is on a specific post using Post ID

If you specify an integer for is_single(), this function will accept it as a Post ID.

// int for post id, string for title and slug
$postId = 1;
if(is_single($postId)){
	echo "This is the Post you Specified!";
}
else{
	echo "This is a NOT the post, It can be another post, or not a post!";
}

Check if the current user is on a post that is one of the specified posts

Specify an array containing mixed values of Post IDs, Slugs, and Titles to check if the current page does belong to one of them.

// int for post id, string for title and slug
$postsToCheck = array(1,"slug-here","the title of a post");
if(is_single($postsToCheck)){
	echo "This is one of Posts you Specified!";
}
else{
	echo "This is a NOT any of these posts, It can be another post, or not a post!";
}

What do you think about "How to Check if the Current Page is a Post in WordPress"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu