If I understand is_single()
correctly, it’s only supposed to return true
when the current “post” (in a generic sense) is a literal post (i.e., it has a post_type
of post
), and that it will return false
when the current “post” is a page, attachment, etc. This is what the Codex says:
is_single() returns true if any single post is being displayed
is_singular() returns true when any page, attachment, or single post is being displayed.
But when I try to link a CSS file only into a post, it’s still included in attachment pages like http://example.com/?attachment=123456789
. Here’s the code I’m using:
<?php if ( is_single() ) { ?>
<link rel="stylesheet" href="style.css" />
<?php } ?>
The Codex page for
is_single
should be updated, what you’re seeing is correct behavior. According to the main Conditional Tags Codex page:You can exclude attachments by also checking
! is_attachment()
.Also, you may want to move that code into an enqueue style instead.
EDIT-
To specifically target single items of a particular post type use
is_singular()
: