Skip to content

Instantly share code, notes, and snippets.

@DamianZaremba
Created August 28, 2011 21:31
Show Gist options
  • Save DamianZaremba/1177249 to your computer and use it in GitHub Desktop.
Save DamianZaremba/1177249 to your computer and use it in GitHub Desktop.
Wordpress wp_nav_menu walker
<?php
/*
* Footer menu walker - just because wordpress sucks
* See wp-includes/class-wp-walker.php for what the hell this does normally (Class Walker)
*/
class footer_menu_walker {
/*
Output we are aiming for:
<div class="col">
<h3>Some sub menu</h3>
<ul>
<li><a href="">Item A</a></li>
<li><a href="">Item B</a></li>
<li><a href="">Item C</a></li>
</ul>
</div>
*/
function __construct() {
$this->wp_walker = new Walker_Nav_Menu();
}
/*
* This builds the menu item and returns it to the walker
*/
function write_menu($element, $depth=0) {
$output = str_repeat("\t", $depth) . '<div class="col">' . "\n";
$output .= str_repeat("\t", $depth+1) . '<h3>' . $element->{'title'} . '</h3>' . "\n";
$output .= str_repeat("\t", $depth+1) . '<ul>' . "\n";
foreach($element->{'children'} as $celement) {
/* No point dublicating this */
$this->wp_walker->start_el(&$output, $celement, $depth+2, array());
/* Damn you wp for bunching stuff together */
$output .= "\n";
}
$output .= str_repeat("\t", $depth+1) . '</ul>' . "\n";
$output .= str_repeat("\t", $depth) . '</div>' . "\n\n";
return $output;
}
/*
* This walks over the elements and splits them into parents or children
*/
function walk($elems) {
$args = array_slice(func_get_args(), 1);
$output = '';
$elements = array();
foreach ( $elems as $e) {
if( $e->{'menu_item_parent'} === "0" ) {
$e->{'children'} = array();
$elements[ $e->{'ID'} ] = $e;
} else {
if( array_key_exists($e->{'menu_item_parent'}, $elements) ) {
$elements[ $e->{'menu_item_parent'} ]->{'children'}[] = $e;
} // If not it might be something other than a 2depth array - we only go to 2
}
}
foreach($elements as $element) {
$output .= $this->write_menu($element, $args[1]->{'depth'});
}
return $output;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment