Advertisement
Guest User

WordPress Link Library Patch to support Hierarchical Links

a guest
Jul 14th, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. function link_library_cats_recursive_func($cats=null) {
  2.     if (!empty($cats)) {
  3.         // A place to store the extended list
  4.         $hierarchy = array();
  5.         // Original selection passed to plugin
  6.         $cats_selected = explode(',', $cats);
  7.         // Ask WordPress for a list of all link categories
  8.         $cats_all = get_terms('link_category');
  9.         // Loop through all passed categories for children
  10.         foreach($cats_selected as $category) {
  11.             // A place to store the children
  12.             $subcat = array();
  13.             // Check all categories to see if their parent was passed to this function
  14.             foreach($cats_all as $cat_obj) {
  15.                 // If so...
  16.                 if ($category == $cat_obj->parent) {
  17.                     // ... collect the id
  18.                     $subcat[] = $cat_obj->term_id;
  19.                 }
  20.             }
  21.             // If we collected any children (id)...
  22.             if (!empty($subcat)) {
  23.                 // ... run this function recursively looking for more
  24.                 $nest = $this->link_library_cats_recursive_func(implode(',', $subcat));
  25.                 // And if we found any more...
  26.                 if (!empty($nest)) {
  27.                     // ... put them with the rest of the children
  28.                     $subcat= array_unique(array_merge($subcat, explode(',', $nest)));
  29.                 }
  30.             }
  31.             // Add the children ids to the extended list of categories
  32.             $hierarchy = array_unique(array_merge($hierarchy, $subcat));
  33.         }
  34.         // Merge list of extended categories with those originally passed
  35.         $result = array_unique(array_merge($hierarchy, $cats_selected));
  36.         // Return list as a comma-seperated list so the plugin can carry on
  37.         return implode(',', $result);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement