Skip to content

Instantly share code, notes, and snippets.

@gearsdigital
Created September 27, 2012 09:35
Show Gist options
  • Save gearsdigital/3793130 to your computer and use it in GitHub Desktop.
Save gearsdigital/3793130 to your computer and use it in GitHub Desktop.
Wordpress Custom Menu
<?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 );
}
}
<?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);
<?php
/**
* Get custom menu (named: context) and list only
* items matched by SplitMenu_Walker_Nav_Menu
*/
$nav_menu_config = array(
'menu' => 'context',
'walker' => new SplitMenu_Walker_Nav_Menu
);
wp_nav_menu($nav_menu_config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment