Fixed hierarchy menu
Showing
1 changed file
with
15 additions
and
5 deletions
| ... | @@ -23,25 +23,35 @@ class HierarchicalMenu implements Countable, Iterator { | ... | @@ -23,25 +23,35 @@ class HierarchicalMenu implements Countable, Iterator { |
| 23 | private $target_lookup = Array(); | 23 | private $target_lookup = Array(); |
| 24 | 24 | ||
| 25 | public function __construct(Array $menu, $parent = 0) { | 25 | public function __construct(Array $menu, $parent = 0) { |
| 26 | $i = 0; | 26 | $last_id = $i = -1; |
| 27 | $last_id = -1; | 27 | $nest = Array(); |
| 28 | 28 | ||
| 29 | foreach ($menu as $key => $item) { | 29 | foreach ($menu as $key => $realitem) { |
| 30 | $item = clone $realitem; | ||
| 30 | $item->_children = false; | 31 | $item->_children = false; |
| 31 | 32 | ||
| 32 | if ($item->menu_item_parent == $parent) { | 33 | if ($item->menu_item_parent == $parent) { |
| 34 | $i++; | ||
| 35 | |||
| 33 | $this->contents[$i] = $item; | 36 | $this->contents[$i] = $item; |
| 34 | $this->id_lookup[$item->ID] = $i; | 37 | $this->id_lookup[$item->ID] = $i; |
| 35 | $this->target_lookup[$item->object_id] = $i; | 38 | $this->target_lookup[$item->object_id] = $i; |
| 36 | 39 | ||
| 37 | $i++; | ||
| 38 | $last_id = $item->ID; | 40 | $last_id = $item->ID; |
| 39 | } elseif ($item->menu_item_parent == $last_id) { | 41 | } elseif ($item->menu_item_parent == $last_id) { |
| 40 | $this->contents[$this->id_lookup[$last_id]]->_children = true; | 42 | $this->contents[$this->id_lookup[$last_id]]->_children = true; |
| 43 | $nest[] = $last_id; | ||
| 44 | |||
| 45 | $last_id = $item->ID; | ||
| 46 | } elseif (count($nest) > 0) { | ||
| 47 | $last_id = array_pop($nest); | ||
| 48 | if (isset($this->id_lookup[$last_id])) { | ||
| 49 | $this->contents[$this->id_lookup[$last_id]]->_children = true; | ||
| 50 | } | ||
| 41 | } | 51 | } |
| 42 | } | 52 | } |
| 43 | 53 | ||
| 44 | foreach ($this->contents as $key => $item) { | 54 | foreach ($this->contents as $key => &$item) { |
| 45 | if (true === $item->_children) { | 55 | if (true === $item->_children) { |
| 46 | $item->HierarchicalMenu = new HierarchicalMenu($menu, $item->ID); | 56 | $item->HierarchicalMenu = new HierarchicalMenu($menu, $item->ID); |
| 47 | } | 57 | } | ... | ... |
-
Please register or sign in to post a comment