1

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 } ?>

2 Answers 2

4

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;
4
  • daamn that sucks! So there's no way to exclude attachments and get css only on posts page? Thanks
    – user20428
    Sep 14, 2012 at 22:32
  • @user20428 - there is, use is_attachment, see my edit.
    – Milo
    Sep 14, 2012 at 22:57
  • 3
    You could also use is_singular('post')
    – Pippin
    Sep 14, 2012 at 23:07
  • fantastic! that works amazingly thank you very much for your time and the help @milo!!!
    – user20428
    Sep 14, 2012 at 23:26
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'
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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