Home WordPress Conditional Functions is_singular() WordPress is_singular() Function Definition In WordPress, is_singular() returns true on any single view . That means a single post, a Page, an attachment, or a single custom post type item.
Pass a post type name, or an array of names, to limit the check. With no argument, it matches all single views.
Syntax Use it as the test of an if statement.
if ( is_singular() ) {
// code for the true path
} else {
// code for the false path
} Call What it does is_singular()True on any single post, Page, attachment, or custom post. is_singular( 'book' )True on a single book. is_singular( array( 'post', 'page' ) )True on a single post or a Page.
A simple way to remember: singular means one item, whatever its type.
Full declaration is_singular( string|string[] $post_types = '' ): bool Parameters Parameter Description $post_typesOptional. A post type name or an array of names. Default is empty, which means any post type.
Return Value Return Value Description bool true if the main query is for a single item of the given type, otherwise false.
WordPress Examples Three short theme examples. Each one shows what prints on the true path and the false path.
1. Any single view 2. One post type 3. Post or Page Example 1 — Any single view Load a script on posts and Pages, but not on archives.
function mytheme_scripts() {
if ( is_singular() ) {
echo '<!-- single-view script -->';
}
}
add_action( 'wp_head', 'mytheme_scripts' ); On a post, a Page, or an attachment (true):
<!-- single-view script -->
On the blog index or an archive (false):
(nothing is printed) How It Works is_singular() is true on every single view.Archives and the blog index return false. The comment is printed only on single views. Example 2 — One post type Show a related box on a single book.
<?php if ( is_singular( 'book' ) ) {
echo '<p>More books by this author</p>';
} ?> On a single book (true):
<p>More books by this author</p>
On a blog post (false):
(nothing is printed) How It Works 'book' is the post type name.Only a single book returns true. Posts and Pages skip the box. Example 3 — Post or Page Match two types with an array.
<?php if ( is_singular( array( 'post', 'page' ) ) ) {
echo '<div class="reading-width">';
} ?> On a post or a Page (true):
<div class="reading-width">
On an attachment or archive (false):
(nothing is printed) How It Works The array holds two post types. The tag is true on a single item of either type. Attachments are not in the list, so they return false. Frequently Asked Questions What does is_singular() return? It returns true or false (a bool). Use it inside an if statement.
What is the difference between is_singular() and is_single()? is_singular() is true on posts, Pages, attachments, and custom post types. is_single() is for posts (and custom post types) but not Pages or attachments.
Does is_singular() work with a custom post type name? Yes. Pass the post type name, for example is_singular( 'book' ).
Is is_singular() true on the blog index? No. The blog index lists many posts, so it is not a single view.
Why does is_singular() not work in functions.php? Conditional tags need the main query. Call them in template files or on hooks such as wp or template_redirect, not directly at the top of functions.php.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful
Helpful Share Copy link Suggestion