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


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..


$blog = get_option('blog_category');

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


Answers (2)


Larrie Bernd Rocha answers:

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

Larrie Bernd Rocha comments:

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.


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=""]]Function reference here:[[/LINK]]