Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function link_library_cats_recursive_func($cats=null) {
- if (!empty($cats)) {
- // A place to store the extended list
- $hierarchy = array();
- // Original selection passed to plugin
- $cats_selected = explode(',', $cats);
- // Ask WordPress for a list of all link categories
- $cats_all = get_terms('link_category');
- // Loop through all passed categories for children
- foreach($cats_selected as $category) {
- // A place to store the children
- $subcat = array();
- // Check all categories to see if their parent was passed to this function
- foreach($cats_all as $cat_obj) {
- // If so...
- if ($category == $cat_obj->parent) {
- // ... collect the id
- $subcat[] = $cat_obj->term_id;
- }
- }
- // If we collected any children (id)...
- if (!empty($subcat)) {
- // ... run this function recursively looking for more
- $nest = $this->link_library_cats_recursive_func(implode(',', $subcat));
- // And if we found any more...
- if (!empty($nest)) {
- // ... put them with the rest of the children
- $subcat= array_unique(array_merge($subcat, explode(',', $nest)));
- }
- }
- // Add the children ids to the extended list of categories
- $hierarchy = array_unique(array_merge($hierarchy, $subcat));
- }
- // Merge list of extended categories with those originally passed
- $result = array_unique(array_merge($hierarchy, $cats_selected));
- // Return list as a comma-seperated list so the plugin can carry on
- return implode(',', $result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement