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

5 from 5
from 1 user
(Rate this post)

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.

if(!function_exists('my_post_date_callback')){

	//register [my-post-date] shortcode
	add_shortcode( 'my-post-date', 'my_post_date_callback' );

	/*
	define [my-post-date] callback
	-> use date-format to define date format
	-> use post-id to specify a post id
	ex: [my-post-date date-format='F j, Y' post-id=1]
	*/
	function my_post_date_callback($attributes) {

		$dateFormat = '';

		$postId = null;

		//check if date-format attribute is present in shortcode
		if(isset($attributes["date-format"]))
			$dateFormat = $attributes["date-format"];

		//check if post-id attribute is present in shortcode
		//and make sure value is numeric
		if(isset($attributes["post-id"]) && is_numeric($attributes["post-id"]))
			$postId = $attributes["post-id"];

		//do not use echo, just use return
		return get_the_date($dateFormat, $postId);
	}
	
}

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 Single Post in WordPress"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu