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_page and 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:

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 the related ones called is_page and 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”, is_page is true. is_single is false, and is_singular is true.
  • If the WordPress content is exactly one “post”: is_page is false, is_single is true, and is_singular is true.
  • If the WordPress content is exactly one of any other post type (CPT): is_page is false, is_single is true and is_singular is true.

Does that make sense? If either is_page or 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 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 the use of is_singular. PHP like:

<?php if ( is_singular() ): ?>
<h2>Popular posts</h2>
        <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>
<?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.

Add a Comment

Your email address will not be published. Required fields are marked *