Understanding is_singular() in WordPress Templates

This text and video Quick Guide explains how to use is_singular(), one of the more useful WordPress template tags. We also explain the difference between the is_singular() conditional tag, and the two other related ones: is_page() and is_single().

is_singular(): One of WordPress’s Conditional Template Tags

WordPress’s conditional tags are little functions that tell you something about the WordPress content being displayed on a page.

For example, is_archive() is another WordPress conditional tag, and it tells you if the content being displayed is a post archive: a list of posts from any WordPress post type, such as the front page of a blog section.

Our full article on WordPress conditional tags can help illuminate the concept, which is key to understanding how is_singular() itself works and what it’s useful for:

How to Use WordPress Conditional Tags

Understanding the Difference Between is_single(), is_singular(), and is_page()

To understand the WordPress conditional tag is_singular(), it’s useful to understand two related tags: is_page() and is_single().

All three tags relate to the condition where only one WordPress post is being displayed. Within that constraint:

  • is_page() will be true only if the piece of content being displayed is a single post of type Page.
  • is_single() is the confusing one. It will be true if the piece of content being displayed is a single post of any post type other than Page or Attachment.
  • is_singular() will be true no matter which the post type of the single piece of content being displayed.

So the three template tags work like this:

  • If the single post is of type Page, then is_page() is true, is_single() is false, and is_singular() is true.
  • If the single post is of type Post, then is_page() is false, is_single() is true, and is_singular() is true.
  • If the single post is of any other post type (a custom post type), then is_page() is false, is_single() is true, and is_singular() is true.

So, if either is_page() or is_single() conditional tags are true, is_singular will always be true.

But if you’re going to display more than one of any post type (including Post and Page), then all of the above conditional tags will be false.

Seeing it in Action: is_single() PHP authoring, with is_singular()

Using is_singular(): Make Something Disappear on Single-Entry HTML Pages

To use the conditional tag is_singular to make something appear when your theme is showing single page or post:

  1. First, find the part of the theme file, say footer.php that you want to control this way. This should be 1-100 lines of HTML and PHP code.
  2. Then, mindful of whether you’ve got PHP tags open or not, add the following line before the content you want to only show when is_singular is true: <?php if ( is_singular() ): ?>
  3. Then, add the following line after the content you want to only show in certain conditions: <?php endif; ?>.

Test it. It should work.

Quick note: saying not in PHP is usually done with a ! (often pronounced “bang” or “exclamation point”). So to show this in the opposite conditions, you’d change the code you added in step #2 to: <?php if ( !is_singular() ): ?>. The easiest way I know to read that is “if is not singular”, although it does literally read more like “if not is singular.” But that’s a little too Yoda-y for my brain.

Example Code Block for is_singular() in WordPress

Finally, if you’re helped by a big single code-block, here’s an example use of is_singular() in PHP:

<?php if ( is_singular() ): ?>
<h2>Popular posts</h2>
    <ul>
        <li><a href="https://wpshout.com/courses/wordpress-development/">Course: Learn WordPress Development</a></li>
        <li><a href="https://wpshout.com/complete-guide-wordpress-security/">Complete Guide to WordPress Security</a></li>
        <li><a href="https://wpshout.com/wordpress-page-builder-review/">WordPress Page Builders, Reviewed</a></li>
    </ul>
<?php endif; ?>

Serious About Learning WordPress Development?

Thanks for learning how is_singular() works in WordPress! If you’d like to understand the entire WordPress PHP ecosystem, and where WordPress template tags fit within it, we strongly recommend you check out our course Up and Running, which teaches WordPress development the clear, thorough, and quick way:

Learn WordPress Development!

Get Up and Running Today

Up and Running is our complete “learn WordPress development” course. Now in its updated and expanded Third Edition, it’s helped hundreds of happy buyers learn WordPress development the fast, smart, and thorough way.

 

I think anyone interested in learning WordPress development NEEDS this course.

Before I purchased Up and Running, I had taught myself some WordPress code, but lacked direction. Watching the course’s videos was like a bunch of lights being turned on.

I went from being vaguely familiar with how themes, functions and WordPress itself worked to mastering these. Everything became much clearer.

I very happily recommend this course to anyone willing to listen.”

–Jason Robie, WordPress developer

Take the next step in your WordPress development journey!


Add a Comment

Your email address will not be published.