DiggingIntoWordPress

by Chris Coyier & Jeff Starr

The Difference Between is_singular() and is_single()

Posted by on

You know that you can target single-view pages with the conditional tag, is_single():

<?php
if(is_single()) {

	// do something

} else {

	// do something else

}
?>

This is a great way to conditionally apply styles, scripts, and markup to single-view pages.

But did you know about the conditional tag, is_singular()? The is_singular() tag enables you to target single-view pages, regular page pages, and attachment pages all in one fell swoop.

So, instead of writing something like this:

<?php
if(is_single() || is_page() || is_attachment()) {

	// do something

} else {

	// do something else

}
?>

We can write this instead:

<?php
if(is_singular()) {

	// do something

} else {

	// do something else

}
?>

The is_singular() tag is what’s known as a “boolean” function, meaning that it returns one of two values, either TRUE or FALSE. No parameters are associated with this tag.

Now you know :)

3 Responses

  1. In the words of Keanu Reeves: Whoa!.

  2. Handy tip, always good to save some extra typing!

  3. I prefer to use (is_single() || is_page() || is_attachment()) cause it’s more readable. But, these little tips are the difference between a pro or a starter guy. Thanks for the tip!

Comments are closed. Contact us with any critical information. Thank you!

Code is poetry