This weekend i needed to extend wordpress basic functionality. I was using the get_the_category function, wich returns an array of objects, one object for each category assigned to the post. What i needed was to display only categories, belonging to exactly one parent.
I search the internet, but surprisingly i didn’t find what i was looking for. So i had to extend wordpress get_the_category function:
function get_the_category_extend($postID, $parent_of=-1){
$out = array();
foreach((get_the_category($postID)) as $category) {
if($parent_of!=-1){
if($category->category_parent == $parent_of){
$out[] = $category;
}
}
}
return $out;
}
It gets post ID and a ID of a parent. It returns only child categories. Yes, its very simple :)
You copy this function into functions.php, located in your theme directory. You can upgrade this function with whatever you need, if your nice you can even post it here…
