views:

575

answers:

2

Hi.

I'm using WordPress 2.8.4.

My question is, if I'm viewing a sub category ("cat-slug-2" in this example) is there a built in function to get it's categoryID and its parents' categoryID?

Here's an example URL, where cat-slug-2 is a sub category of cat-slug-1

http://www.foo.com/category/cat-slug-1/cat-slug-2/

Thanks, Jon

A: 

Maybe something like this?

<?php
    $current_category = single_cat_title("", false);
    $category_ID = $wpdb->get_var( "SELECT term_id FROM $wpdb->terms WHERE slug = '" . $current_category . "'" );
    echo(get_category_parents($category_ID, TRUE, ' &raquo; '));
?>

For more info on the WP Functions/Template Tags used above, see single_cat_title and get_category_parents.

Manzabar
A: 

Thanks for the answer Manzabar, from your code i was able to modify it to get what i wanted.

Ultimately, I wanted an array of the category's parentIDs. Here's how I did it:

$parents = get_category_parents($cat, false, '^%%^');
$parents = explode('^%%^', $parents);
$parentIDs = array();
foreach($parents as $parent){
    if (is_null($parent) || empty($parent)){
        continue;
    }
    $parentIDs[] = get_cat_ID($parent);
}
echo '<pre>';
print_r($parentIDs);
echo '</pre>';

Note that $cat holds the current categoryID

Jon Reeks