[wp-trac] [WordPress Trac] #10277: How to get root category
WordPress Trac
wp-trac at lists.automattic.com
Fri Jun 26 07:42:59 GMT 2009
#10277: How to get root category
-------------------------+--------------------------------------------------
Reporter: bi0xid | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Unassigned
Component: General | Version: 2.9
Severity: normal | Keywords: root category
-------------------------+--------------------------------------------------
I have looking for some function that allow me to know whick is the root
category of one given category when we have a category tree.
I found only how to get the parent category, but not the root, so I made
this function for one of my projects:
{{{
function get_category_root($category_id){
$category = $category_id->cat_ID;
global $wpdb;
$parent = $wpdb->get_var( "SELECT parent FROM $wpdb->term_taxonomy
WHERE term_id = '$category'");
if ($parent == 0) {
return $category;
}
while ($parent != 0){
$category = $parent;
$parent = $wpdb->get_var( "SELECT parent FROM
$wpdb->term_taxonomy WHERE term_id = '$category'");
if ($parent == 0) {
return $category;
}else{
$category = $parent;
}
}
}
}}}
Maybe it's useful.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/10277>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list