Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 } ?>
share|improve this question

2 Answers 2

To specifically target single items of a particular post type use is_singular():

if( is_singular( 'post' ) ) {
    // Only execute this block on a single page for a post with type 'post'
}
share|improve this answer
    
thank you for the response! –  user20428 Sep 14 '12 at 23:33

The Codex page for is_single should be updated, what you're seeing is correct behavior. According to the main Conditional Tags Codex page:

is_single()

When any single Post (or attachment, or custom Post Type) page is being displayed. (False for Pages)

You can exclude attachments by also checking ! is_attachment().

Also, you may want to move that code into an enqueue style instead.

EDIT-

if ( is_single() && ! is_attachment() ) :
    echo 'is a single post and not an attachment';
endif;
share|improve this answer
    
daamn that sucks! So there's no way to exclude attachments and get css only on posts page? Thanks –  user20428 Sep 14 '12 at 22:32
    
@user20428 - there is, use is_attachment, see my edit. –  Milo Sep 14 '12 at 22:57
3  
You could also use is_singular('post') –  Pippin Sep 14 '12 at 23:07
    
fantastic! that works amazingly thank you very much for your time and the help @milo!!! –  user20428 Sep 14 '12 at 23:26
    
@Pippin that also works great! thank you –  user20428 Sep 14 '12 at 23:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.