14

When I'm using is_single(); in my <head> section to add some style to website navigation it executes correctly on blog posts but it also executes on single "portfolio" post type posts (so single-portfolio.php and single.php).

How do I make it execute only on single.php?

2 Answers 2

28

You can use the following instead,

if (is_singular('post')) {

    //your code here...

}

Where by is_singular is the WordPress API conditional function for testing for the existence of a post type. You can also pass an array of post types if you wish.

http://codex.wordpress.org/Function_Reference/is_singular

2
  • 1
    That's another very good answer! Both answers resolve this issue. This one seems to be simpler. Thanks!
    – Paul
    Jul 28, 2012 at 15:19
  • 1
    Thank you, always best to use built-in functions. I think this should be the accepted answer.
    – Simon
    Jul 28, 2012 at 15:19
8

You could try something like (http://codex.wordpress.org/Function_Reference/get_post_type) :

to check if it's not a specific post-type :

if ( is_single() && 'portfolio' != get_post_type() ) {
  // DO STUFF
}

or to check if it's a post and not a post-type :

if ( is_single() && 'post' == get_post_type() ) {
  // DO STUFF
}
3
  • Thanks! That resolves my problem BUT what if I have "slideshow", "news", "portfolio" and a lot more post types? Some can be extended using plugins and I'm not sure how many there will be and what will be their names. Is there a way to do it without excluding just "portfolio" but everything that is not a blog post?
    – Paul
    Jul 28, 2012 at 15:15
  • @Flow: use get_post_type() to check for 'post'.
    – Geert
    Jul 28, 2012 at 15:17
  • I have updated my answer ^^
    – Simon
    Jul 28, 2012 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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