I am writing my own theme and I came up on a PHP problem. Here is the original default.php i was working with which worked well for what it did:
The problem is, I am changing my menu style and I now need to differentiate between the first menu item, the last, and any in between (to add in a CSS class). So here is what I did with the code but it is not working:Code:public function process($module, $element) { self::_process($module, $element->first('ul:first')); return $element; } /* Function: _process Returns: Void */ protected static function _process($module, $element, $level = 0) { if ($level == 0) { $element->attr('class', 'menu '.$module->menu_style); } else { $element->addClass('level'.($level + 1)); } foreach ($element->children('li') as $li) { // is active ? if ($active = $li->attr('data-menu-active')) { $active = $active == 2 ? ' active current' : ' active'; } // is parent ? $ul = $li->children('ul'); $parent = $ul->length ? ' parent' : null; // set class in li $li->attr('class', sprintf('level%d item%s'.$parent.$active, $level + 1, $li->attr('data-id'))); // set class in a/span foreach ($li->children('a,span') as $child) { // get title $title = $child->first('span:first'); // set subtile $subtitle = $title ? explode('||', $title->text()) : array(); if (count($subtitle) == 2) { $li->addClass('hassubtitle'); $title->html(sprintf('<span class="title">%s</span><span class="subtitle">%s</span>', trim($subtitle[0]), trim($subtitle[1]))); } // set image if ($image = $li->attr('data-menu-image')) { $title->prepend(sprintf('<span class="icon" style="background-image: url(\'%s\');"> </span>', $image)); } $child->addClass(sprintf('level%d'.$parent.$active, $level + 1)); } // process submenu if ($ul->length) { self::_process($module, $ul->item(0), $level + 1); } }
Code:public function process($module, $element) { self::_process($module, $element->first('ul:first')); return $element; } /* Function: _process Returns: Void */ protected static function _process($module, $element, $level = 0) { if ($level == 0) { $element->attr('class', 'menu '.$module->menu_style); } else { $element->addClass('level'.($level + 1)); } foreach ($element->children('li') as $li) { // is active ? if ($active = $li->attr('data-menu-active')) { $active = $active == 2 ? ' active current' : ' active'; } // is parent ? $ul = $li->children('ul'); $parent = $ul->length ? ' parent' : null; // is first or last ? $lis = $element->children("ul"); for($forl=0,$imax=count($lis); $forl<$imax;$forl++){ $forl = array(); if (forl==0) $position_n = 'first'; elseif ($forl==$imax-1) $position_n = 'last'; else $position_n = null; } // set class in li $li->attr('class', sprintf('level%d item%s'.$parent.$active, $level + 1, $li->attr('data-id'), $position_n)); // set class in a/span foreach ($li->children('a,span') as $child) { // get title $title = $child->first('span:first'); // set subtile $subtitle = $title ? explode('||', $title->text()) : array(); if (count($subtitle) == 2) { $li->addClass('hassubtitle'); $title->html(sprintf('<span class="title">%s</span><span class="subtitle">%s</span>', trim($subtitle[0]), trim($subtitle[1]))); } // set image if ($image = $li->attr('data-menu-image')) { $title->prepend(sprintf('<span class="icon" style="background-image: url(\'%s\');"> </span>', $image)); } $child->addClass(sprintf('level%d'.$parent.$active, $level + 1)); } // process submenu if ($ul->length) { self::_process($module, $ul->item(0), $level + 1); } }


Reply With Quote



Bookmarks