I have tried every possible way of making this conditional work and it's skipping the in_category conditionals and going to the last option. This is my code in single.php:

$post = $wp_query->post;
if (in_category('Autos')) {
} else if (in_category('Advertising')) {
} else {

I have conditionals all over this theme that work by calling the in_category for those two categories, but no matter what I do, this conditional always reverts to the single-post.php layout. single-autos.php and single-ads.php do exist in the theme.

Is there something I'm doing wrong?


Edited to add the code from single-autos.php:

<div id="blogcontent" class="auto">

     if (have_posts()) : while (have_posts()) : the_post(); 

          get_template_part( 'loop' );


    <div id="singlepagination">
        <div class="newer">< next_post_link(); > </div>
        <div class="older">< previous_post_link(); > </div>

     endwhile; else: 

        <h2>Thank you for pushing the self-destruct button. This ship will self destruct in ten seconds.</h2>


  • Try to echo something like A/B/C inside the if/elseif/else to see that it's not a typo or similar. – kaiser Feb 3 '12 at 6:51
  • Are you using the conditionals in a standard loop? I suspect that's the issue because usually you retrieve the global $post object rather than setting $post within the template. – Brian Fegter Feb 3 '12 at 9:11
  • Where - i.e. in what template file - is this code? – Chip Bennett Feb 3 '12 at 12:47
  • Perhaps I wasn't clear - I said this in my first paragraph... This is in single.php :) – m.j. Feb 3 '12 at 15:10
  • I'm not sure it's relevant to your question, but your markup has syntax errors; namely, you're not closing/opening PHP tags properly between PHP output and HTML output. – Chip Bennett Feb 3 '12 at 15:27

My first suspicion that something is amiss is this:

$post = $wp_query->post;

You shouldn't need to do this. Anywhere that the query conditionals are already setup, the conditionals will assume the current Post ID. So, that suggests a couple things:

  1. The specified categories don't actually exist. I will assume this not to be true, since you indicate that you use this same code successfully elsewhere in the Theme.
  2. You're calling this code somewhere that the current query/post isn't actually set. For example, you might be calling this code in the context of an archive index page, or a search page, etc. outside the Loop. The odd thing here is that your title indicates that you're calling this code in single.php.

Can you do two things:

  1. Post your entire single.php code, so we can see the conditional code in context?
  2. Try replacing $post = $wp_query->post; with global $post;


What's posted above is the entire single.php

What's being returned by the $post object? What do you get on a var_dump( $post ); after you globalize it?

Also, probably not related to your problem (the query is set at wp, so query conditionals should be available at template_redirect, which is where single.php gets loaded), but I would suggest putting your get_header(), get_sidebar(), and get_footer() calls in single.php, and trim single-auto.php, etc. down to their actual content.

  • What's posted above is the entire single.php: <?php global $post; if (in_category('Autos')) { get_template_part("single-autos.php"); } else if (in_category('Advertising')) { get_template_part("single-ads.php"); } else { get_template_part("single-post.php"); } ?> – m.j. Feb 3 '12 at 15:12
  • At the moment single-autos.php, single-ads.php and single-post.php are exactly the same except for one class to change the background and colours. I'm posting in the original question, as there's not enough room here... – m.j. Feb 3 '12 at 15:15
  • See updated answer. Are you getting anything returned by the globalized $post object? – Chip Bennett Feb 3 '12 at 15:40
  • I don't think so... I took it out completely. I was using a tutorial I found here which I realize is insanely old, but worked the last time I used it. – m.j. Feb 3 '12 at 15:48
  • But did you test it? If you're not getting anything returned from the $post object, then that observation is probably directly related to your problem. – Chip Bennett Feb 3 '12 at 16:19

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.