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.

So I'm trying to do an if / else statement on an archive.php template. I setting up my archives using WP Smart archives reloaded (http://wordpress.org/extend/plugins/smart-archives-reloaded/)

The code from my archive.php is as follows:

<?php
   if (in_category('166')) { include 'archive-blog.php';
}

else {
   echo('foo');
}
?>

Im trying to get archive.php to include a certain template based on the category number. However this code grabs the archive blog template no matter what (even if I click on a category archive that is not the blog).

I'm wondering if I'm not doing this correctly - does in_category not work on an archive page? I have a similar set up with a single.php directing to a different include template based on the category.

Thanks!

Update

I've also tried to use is_category and it seems to be completely ignoring my first condition and just echoing foo for any category.

<?php
   if (is_category('166')) { include 'archive-blog.php';
}

else {
   echo('foo');
}
?>

Link to what I'm talking about: (go into the archives in the right sidebar)

http://www.metropoliscreative.com/talent-analytics/w/?cat=166

http://www.metropoliscreative.com/talent-analytics/w/?cat=3

also, im not using permalinks because the clients server does not have mod-rewrite

share|improve this question
add comment

5 Answers

If memory serves, in_category() is for use within the loop and returns true when the current post is in that particular category.

You're probably for is_category(166).

share|improve this answer
    
Hi Denis, see my update above. That does not seem to work... –  nikibrown Feb 7 '11 at 19:51
    
lol i started to write the answer went answer a phone call and i see your posted answer. your memory serves you wrong in_category is not only for within the loop. –  Bainternet Feb 7 '11 at 19:53
    
lol, depends on how long you tackle with Wordpress: "This tag can be used to test the current post within The Loop or (since Version 2.7) outside the Loop during a single post request." Reference - and Denis does since a longer time. ;) –  hakre Feb 9 '11 at 0:35
add comment

Alternatively you could just name your file so that WordPress knows to use it for that category. According to the Codex, WordPress looks for the following files (in this order) in order to display your categories:

  1. category-slug.php
  2. category-ID.php
  3. category.php
  4. archive.php
  5. index.php

So if you named/created a template file called category-166.php, then WordPress would use that file to display category 166.

share|improve this answer
add comment

you are almost there its not in_category('166') its is_category('166') the difference are:

  • in_category checks if single is in that category.
  • is_category check if current is that category archive page.
share|improve this answer
add comment
<?php
$post = $wp_query- >post;
if ( in_category('166') ) {
include(TEMPLATEPATH . '/archive-blog.php’);
} else {
   echo('foo');
} ? >

is that what you're looking for?

share|improve this answer
add comment

Try is_category() instead.

Example. Check if in category 12 OR 13

<?php if ( is_category('12') || is_category('13')) {  ?>

    TEST

<?php }   ?>
share|improve this answer
add comment

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.