Get parent category when in \child\child\ category
-
Hi,
I’d like to know how to get the ID of the parent category when having sub-child categories.
——————–
I want this:
When in this category: \parent\child\child\
or \parent\ or \parent\child\
Give ID of: \parent\
——————–
The code below only works for: \parent\child\
——————-
$this_category = get_category($cat);
// If category is parent, list it –>
if ($this_category->category_parent == 0) {
$menu_cat = $this_category->cat_ID; //cat_name
$this_category->category_parent = $cat;
} else {
// If category is not parent, list parent category –>
$parent_category = get_category($this_category->category_parent);
$menu_cat = $parent_category->cat_ID; // cat_name
// echo $menu_cat;——————-
I hope somebody understands and is willing to help. Thanks in advance.
- The topic ‘Get parent category when in \child\child\ category’ is closed to new replies.