Huh, doesn’t seem to be doing it…
Code:
<?php if(in_category('Sponsored Posts') == true) { ?>
hello there
<?php } ?>
Example URL: http://www.ensight.org/archives/2003/08/20/test-ad-post
Am I doing something wrong?
You aren’t doing it wrong – the “in_category” function isn’t a template friendly function it seems. It checks for category based on category ID, instead of name. I rewrote it to check via either attribute, if you would like.
Open up wp-includes/template-functions-category.php and goto the end, you will see the
function in_category($categor) {
Replace that whole function with:
function in_category($category, $by='id') { // Check if the current post is in the given category
// updated by Jeff Minard to include a keyword search ability,
// cause nobody is going to check via category number - duh.
global $post, $category_cache;
$cats = '';
foreach ($category_cache[$post->ID] as $cat) :
if($by == 'id') {
$cats[] = $cat->category_id;
} else if ($by == 'name') {
$cats[] = $cat->cat_name;
}
endforeach;
if ( in_array($category, $cats) )
return true;
else
return false;
}
THEN this should work:
<?php if( in_category(‘General’, ‘name’) ) { ?>
it is in the General Category
<?php } ?>
A few things to note:
- This function is backwards compatible, so no worries there.
- Category names are case sensitive, so be careful to quote it correctly.
Well, ok, by a few I meant two – but still. Good luck!
Awesome, that worked perfectly, thanks!!!
No problem, this is something that should be submitted as a bug or something like that. Hrm…