Understanding is_singular in WordPress Templates
Let’s talk about is_singular, one of WordPress’s more complicated template tags. In WordPress themes (and plugins too), we use “conditional tags” to control what shows up. This Quick Guides explains how to use the
is_singular conditional tag in WordPress, as well the two other related ones:
is_single. As you might guess, they all tell use something about whether or not there is one piece of WordPress content. If it is (a) singular piece of content 🤓
If you’re new to the idea of “conditional tags,” they’re WordPress-provided functions that tell you something about the WordPress content being displayed on a page. From the other direction, they tell you things from about the world from your location in a WordPress theme file. So
is_singular PHP tells you what HTML it should show. Understanding conditional tags is something that makes the most sense after you’ve dabbled with WordPress theming a little. If you’re brand new at it I think our article on conditional tags fleshes it out a bit more:
Understanding the Difference Between
To understand the WordPress conditional tag,
is_singular, it’s useful to understand the related ones called
is_single. So here’s a quick summary: all three relate to the condition where one (and not 2, 10, 1000, etc) piece of WordPress content is shown. Then they work like this:
- If the WordPress content is exactly one “page”,
- If the WordPress content is exactly one “post”:
- If the WordPress content is exactly one of any other post type (CPT):
Does that make sense? If either
is_single conditional tags are true,
is_singular will always be true. But if you’re going to see more than one of *any* post type (including post and page), ALL of the above conditional tags will be
false. The nuances between them exist when you have precisely one “post” showing.
Seeing it in Action:
is_single PHP authoring, with
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:
- First, find the part of the theme file, say
footer.phpthat you want to control this way. This should be 1-100 lines of HTML and PHP code.
- 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
<?php if ( is_singular() ): ?>
- 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 the use of
is_singular. PHP like:
<?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; ?>
Again, the line before and after this were pure HTML. In PHP, the need to open and close your tags is not absolute.