Like the blog? Get the book »

The Difference Between is_singular() and is_single()

The Difference Between is_singular() and is_single()

With WordPress, there are various distinct types of page views. For example, there are category-archive views, tag-archive views, author-archive views, date-archive views, single-post views, single-page views, and so forth. And of course you know that you can target single-post views in your theme template 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 for this post. Contact us with any critical information.
© 2009–2021 Digging Into WordPress Powered by WordPress Monzilla Media shapeSpace