Created
September 27, 2012 09:35
-
-
Save gearsdigital/3793130 to your computer and use it in GitHub Desktop.
Wordpress Custom Menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Split the menu to top level items and the children of the | |
* current active parent. | |
* @see http://helperclass.blogspot.de/2011/11/creating-separate-sub-menu-in-wordpress.html | |
*/ | |
class SplitMenu_Walker_Nav_Menu extends Walker_Nav_Menu { | |
private $menu = false; | |
public function start_lvl(&$output, $depth) { | |
return false; | |
} | |
public function end_lvl($output, $depth) { | |
$this->menu = false; | |
} | |
public function start_el(&$output, $item, $depth, $args) { | |
if(($args->has_children && $item->current) || $item->current_item_parent) { | |
$this->menu = true; | |
} | |
if($this->menu && $depth > 0){ | |
parent::start_el($output, $item, $depth, $args); | |
} | |
} | |
public function end_el(&$output, $item, $depth) { | |
if( $this->menu ) { | |
parent::end_el($output, $item, $depth); | |
} | |
} | |
public function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) { | |
$id = $this->db_fields['id']; | |
if ( is_object( $args[0] ) ) { | |
$args[0]->has_children = ! empty( $children_elements[$element->$id] ); | |
} | |
return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get custom menu (named: context) | |
* and list only first level items (depth=1) | |
*/ | |
$nav_menu_config = array( | |
'menu' => 'context', | |
'depth'=> 1 | |
); | |
wp_nav_menu($nav_menu_config); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment