Skip to content

WPShout Newsletter

Sign up for news, tips, and insights to help you build better websites.

Newsletter - Top banner

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 an archive page: a list of posts grouped by category, tag, author, date, custom post type, or custom taxonomy. The main blog posts index is a different case, and is_home() detects it.

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. It usually returns true for attachments too, but that behavior is not guaranteed, so use is_attachment() || is_single() when attachments should count.
  • is_singular() will be true no matter which post type the single piece of content being displayed has. You can also pass a post type or an array of post types, such as is_singular( 'book' ), to check for specific types only.

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 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.

These tags describe the main query of the current request, not how many posts a template happens to render. On list views such as the blog posts index, archives, and search results, all three are false.

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

Using is_singular(): Show Something on Single-Entry Pages

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

  1. First, in a classic PHP-based theme, 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. Block themes use HTML templates and template parts such as parts/footer.html instead, so there the check goes in a function hooked to an action that runs after the main query, because a conditional tag used before the query has run has nothing to test.
  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. If a tag works in the header but not in the footer, a custom query in another template, such as sidebar.php, may have changed it: call wp_reset_query() just before the conditional test.

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/learn-wordpress-development-the-basic-course/">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-conditional-tags/">How to Use WordPress Conditional Tags</a></li>
    </ul>
<?php endif; ?>

Thanks for learning how is_singular() works in WordPress!

OPEN FOR ENROLLMENT

Join Modern WordPress Fast Track today!

02:04 min • Watch the course overview

Yay! 🎉 You made it to the end of the article!
David Hayes
Share:

0 Comments
Most Voted
Newest Oldest