Skip to content

Commit

Permalink
Fix menu when only showing 2 levels.
Browse files Browse the repository at this point in the history
When showing only 2 levels in the top dropdown menu, 2nd level
menu items with children need to "skip ahead" to the first child
item since the 2nd level item wouldn't have a page of its own.
  • Loading branch information
jimt committed Nov 12, 2015
1 parent 141a245 commit c12636a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 54 deletions.
113 changes: 60 additions & 53 deletions inc/Walker_OERU_Menu_Depth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

class Walker_OERU_Menu_Depth extends Walker {

// Tell Walker where to inherit it's parent and id values
var $db_fields = array(
'parent' => 'menu_item_parent',
'id' => 'db_id'
);
// Tell Walker where to inherit its parent and id values
var $db_fields = array(
'parent' => 'menu_item_parent',
'id' => 'db_id'
);

var $first = false;
var $fixup = false;


function start_lvl( &$output, $depth = 0, $args = array() ) {
Expand All @@ -32,7 +32,6 @@ function end_lvl( &$output, $depth = 0, $args = array() ) {
}

function end_el( &$output, $object, $depth = 0, $args = array() ) {

if($depth<=0){

$output .= "</li>";
Expand All @@ -42,61 +41,69 @@ function end_el( &$output, $object, $depth = 0, $args = array() ) {
}

function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

if($depth<=1){

$page_class = "";

foreach($item->classes as $index => $class){

if(strpos($class,"-")===FALSE){
$page_class = "";

$page_class .= $class . " ";
unset($item->classes[$index]);
foreach($item->classes as $index => $class){

if(strpos($class,"-")===FALSE){

$page_class .= $class . " ";
unset($item->classes[$index]);

}

}

}

$item->classes[] = str_replace(" ", "-", strtolower(trim($page_class)));

if(in_array("menu-item-has-children",$item->classes)){
$item->classes[] = "dropdown";
}

if($item->menu_item_parent!=0){
$item->classes[] = "sub-menu";
}else{
$item->classes[] = "parent-menu";
}

if($depth !=0){
if(($key = array_search("menu-item-has-children", $item->classes)) !== false) {
unset($item->classes[$key]);
}
}

$output .= "<li class='" . implode(" ", $item->classes) . "'><a ";
if ( $item->object_id === get_the_ID() ) {
$output .= " class='current' ";
}

if($depth == 0){
$output .= " href='#' data-toggle='dropdown' class='dropdown-toggle'>" . $item->title;
}else{
$output .= " href='" . $item->url . "'>" . $item->title;
}

if($depth==0){

$item->classes[] = str_replace(" ", "-", strtolower(trim($page_class)));

if(in_array("menu-item-has-children",$item->classes)){
$output .= "<span class='dropdown caret'></span>";
$item->classes[] = "dropdown";
}

if($item->menu_item_parent!=0){
$item->classes[] = "sub-menu";
}else{
$item->classes[] = "parent-menu";
}

if($depth !=0){
if(($key = array_search("menu-item-has-children", $item->classes)) !== false) {
unset($item->classes[$key]);
}
}

$output .= "<li class='" . implode(" ", $item->classes) . "'><a ";
if ( $item->object_id === get_the_ID() ) {
$output .= " class='current' ";
}

if($depth == 0){
$output .= " href='#' data-toggle='dropdown' class='dropdown-toggle'>" . $item->title;
}else{
if ( in_array( 'dropdown', $item->classes ) ) {
$output .= ' href="-OERU--SECOND-LEVEL-MENU">' . $item->title;
$fixup = true;
} else {
$output .= " href='" . $item->url . "'>" . $item->title;
}
}

if($depth==0){

if(in_array("menu-item-has-children",$item->classes)){
$output .= "<span class='dropdown caret'></span>";
}

}

$output .= "</a>";

}

$output .= "</a>";

} else if ( fixup && ( $depth == 2 ) ) {
// backfill the last first second level URL with the first third level one
$output = str_replace( '-OERU--SECOND-LEVEL-MENU', $item->url, $output );
$fixup = false;
}

}
Expand Down
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Theme Name: OERu Course Theme
Author: Pgogy Webstuff
Author URI: http://pgogywebstuff.com
Description: Open Class Theme for the OERu
Version: 0.5.8
Version: 0.6.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: fixed-layout, responsive-layout, custom-background, custom-header, custom-menu, editor-style, featured-images, flexible-header, full-width-template, rtl-language-support, sticky-post, theme-options
Expand Down

0 comments on commit c12636a

Please sign in to comment.