21 Apr 2010

WordPress Hacks : Get the depth of a category or Category depth

Web Design, Development Tips & Tutorials 1 Comment

WordPress doesn’t have a function to get the depth of a category. Here is  a function to get the depth of a category,  I wrote a while back with the help of Jens Törnell code:

function get_category_depth($id = '', $depth = '', $i = 0)
{
global $wpdb;
global $term_taxonomy;
if($depth == '')
{
if(is_category())
{
if($id == '')
{
$id = $term_taxonomy->term_id;
}
$depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$id."'");
return get_category_depth($id, $depth, $i);
}
}
elseif($depth == "0")
{
return $i;
}
else
{
$depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$depth."'");
$i++;
return get_category_depth($id, $depth, $i);
}
}
One Response to “WordPress Hacks : Get the depth of a category or Category depth”

Leave a Reply

Web Design Nairobi,Kenya Website Design, Website Design Kenya,Nairobi Website Design