Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Is current category in comma separated list of IDs? WordPress

  • SOLVED

Hopefully this is a quick one.

I need to check if the current category is one of a comma separated list of IDs.

Below is what I have working correctly right now but the value of <strong>get_option('blog_category');</strong> is only a single ID (i.e. 192). I need it to work if the value is 192,128,119, etc..

<?php

$blog = get_option('blog_category');

if ( is_category($blog)) )
include (TEMPLATEPATH . '/blog.php');
else
include (TEMPLATEPATH . '/photo.php');

?>

Answers (2)

2010-12-02

Larrie Bernd Rocha answers:

$cats = explode(",", get_option('blog_category'));
if(in_category($cats))
include (TEMPLATEPATH . '/blog.php');
else
include (TEMPLATEPATH . '/photo.php');


Larrie Bernd Rocha comments:

Explanation:
Explode will convert your comma-separated category IDs to an array of category IDs. in_category will test if the current post is under the category ID, the category name or in an array of category IDs.


Joe Calithia comments:

Perfect Larrie thank you!

@Pippen - The IDs will change so that wouldn't be work.

2010-12-02

Pippin Williamson answers:

Use the in_category() function.

So, you could do something like this:


<?php if ( in_category( array( 'fruits', 'apples', 'bananas', 'cantaloupes', 'guavas', /*etc*/ ) )) {
// These are all fruits
// do your include here
} else {
// do your other include here
}
?>


[[LINK href="http://codex.wordpress.org/Function_Reference/in_category#Parameters"]]Function reference here:[[/LINK]]