php - WP_Nav_Walker extending, need to know count of Sub Menu Items -


i have 2 different displays here. 1 mobile , desktop. need output different html both 1 menu item (the last 1) in menu. example, given following menu structure:

- menu item 1 -- child 1 -- child 2 -- child 3 - menu item 2 -- child 1 -- child 2 -- child 3 -- child 4 - menu item 3 -- child 1 -- child 2 -- child 3 -- child 4 -- child 5 -- child 6 

i need able output following structure desktop:

<ul>     <li>menu item 1     <ul>         <li>child 1</li>         <li>child 2</li>         <li>child 3</li>     </ul>     </li>     <li>menu item 2     <ul>         <li>child 1</li>         <li>child 2</li>         <li>child 3</li>         <li>child 4</li>     </ul>     </li>     <li>menu item 3     <ul>         <li>child 1</li>         <li>child 2</li>         <li>child 3</li>     </ul>     </li>     <li>&nbsp;     <ul>         <li>child 4</li>         <li>child 5</li>         <li>child 6</li>     </ul>     </li> </ul> 

the last parent menu item $depth === 0 needs split in half separate items. on mobile display, not need split in half, should display perfect mobile.

so have 2 different wp_nav_walker extending classes. 1 mobile , desktop handle menu differently, problem i'm facing how total count of submenu items. understand how know if menu has children or not using $args->has_children, how total number of children??

i've created variable know sub-menu item i'm on within end_el function:

class custom_nav_walker extends walker_nav_menu {      function __construct() {         $this->boxitem_index = 0;     }      public function start_lvl(&$output, $depth = 0, $args = array())     {         if ($depth === 0 && $this->menu_type == 'header')         {             echo '<pre>', var_dump($item), '</pre>';         }     } } 

i have set custom property called menu_type gets set within start_el function attaches last menu here, i'm able know it's menu want.

basically, need split in half, not 3, i'm not able know half is. , ofcourse, don't want perform if statement on last item. so, know how many items have $depth === 1 last parent menu. possible?

the output $item this:

object(wp_post)#723 (40) {   ["id"]=>   int(73)   ["post_author"]=>   string(1) "1"   ["post_date"]=>   string(19) "2016-07-14 18:09:44"   ["post_date_gmt"]=>   string(19) "2016-07-14 18:09:44"   ["post_content"]=>   string(0) ""   ["post_title"]=>   string(11) "quick links"   ["post_excerpt"]=>   string(0) ""   ["post_status"]=>   string(7) "publish"   ["comment_status"]=>   string(6) "closed"   ["ping_status"]=>   string(6) "closed"   ["post_password"]=>   string(0) ""   ["post_name"]=>   string(13) "quick-links-3"   ["to_ping"]=>   string(0) ""   ["pinged"]=>   string(0) ""   ["post_modified"]=>   string(19) "2016-07-21 17:12:08"   ["post_modified_gmt"]=>   string(19) "2016-07-21 17:12:08"   ["post_content_filtered"]=>   string(0) ""   ["post_parent"]=>   int(0)   ["guid"]=>   string(30) "http://0b90b21a.ngrok.io/?p=73"   ["menu_order"]=>   int(12)   ["post_type"]=>   string(13) "nav_menu_item"   ["post_mime_type"]=>   string(0) ""   ["comment_count"]=>   string(1) "0"   ["filter"]=>   string(3) "raw"   ["db_id"]=>   int(73)   ["menu_item_parent"]=>   string(1) "0"   ["object_id"]=>   string(2) "73"   ["object"]=>   string(6) "custom"   ["type"]=>   string(6) "custom"   ["type_label"]=>   string(11) "custom link"   ["title"]=>   string(11) "quick links"   ["url"]=>   string(0) ""   ["target"]=>   string(0) ""   ["attr_title"]=>   string(0) ""   ["description"]=>   string(0) ""   ["classes"]=>   array(5) {     [0]=>     string(0) ""     [1]=>     string(9) "menu-item"     [2]=>     string(21) "menu-item-type-custom"     [3]=>     string(23) "menu-item-object-custom"     [4]=>     string(22) "menu-item-has-children"   }   ["xfn"]=>   string(0) ""   ["current"]=>   bool(false)   ["current_item_ancestor"]=>   bool(false)   ["current_item_parent"]=>   bool(false) } 

you can use filter function 2 additional properties _children_count , _parent_children_count on each item, can later evaluate in menu walker:

function my_wp_nav_menu_objects_filter($sorted_menu_items) {     foreach($sorted_menu_items &$item) {         $item->_children_count = 0;         for($i=1, $l=count($sorted_menu_items); $i<=$l; ++$i) {             if($sorted_menu_items[$i]->menu_item_parent == $item->id) {                 $item->_children_count++;             }         }             }     foreach($sorted_menu_items &$item) {         $item->_parent_children_count = 0;         for($i=1, $l=count($sorted_menu_items); $i<=$l; ++$i) {             if($item->menu_item_parent == $sorted_menu_items[$i]->id) {                 $item->_parent_children_count = $sorted_menu_items[$i]->_children_count;                 break;                                 }         }     }     unset($item);     return $sorted_menu_items;     } add_filter('wp_nav_menu_objects', 'my_wp_nav_menu_objects_filter' ); 

weirdly enough, looks $sorted_menu_items array numerically indexed starting @ 1.
added _ in front of new properties pseudo-namespacing, avoid collisions possible future official properties.


Comments