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…

Comments

  • kurt

    Peter. How did you achieve this? Did you simply add to your template a tag,

    ?

    And did you specify anywhere the category name or ID?

    I want to do the same, simply list Sub-Category Names on separate pages, but in a lost of different boxes on the page. Something like this:

    Sub-Cat Name 1
    Sub-Cat Name 2
    Sub-Cat Name 3
    Sub-Cat Name 4
    Sub-Cat Name 5

    Any ideas?

    Kurt

  • peterw

    Hello kurt. I think you can use this function to solve your problem – just insert the parent id of sub categories…

Leave a Comment

© Copyright Webspices.net. All Rights Reserved.