-
-
Save jpgninja/17421d260ae9003e5f51509f99596721 to your computer and use it in GitHub Desktop.
Custom WordPress Walker for Michael McNeill
This file contains hidden or 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 | |
/** | |
* Custom Menu Walker | |
* | |
* Author: Chris Mewhort | |
* Author URI: http://chrismewhort.com | |
* Walker URI: https://codeable.io/developers/chris-mewhort | |
* | |
* Reference Markup URI: https://gist.github.com/michaelryanmcneill/381ed1a28726fe0eacb0973a7de40b92 | |
*/ | |
class Codeable_Walker_Menu extends Walker_Nav_Menu { | |
// Tell Walker where to inherit it's parent and id values | |
var $db_fields = array( | |
'parent' => 'menu_item_parent', | |
'id' => 'db_id' | |
); | |
function start_lvl( &$output, $depth = 0, $args = array() ) { | |
} | |
function end_lvl( &$output, $depth = 0, $args = array() ) { | |
} | |
/** | |
* At the start of each element | |
* | |
*/ | |
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { | |
if ($depth === 0) { | |
$output .= sprintf( "\n | |
<div class=\"%s\"> | |
<div class=\"%s\"> | |
<a class=\"accordion-toggle\" data-toggle=\"collapse\" data-parent=\"%s\" href=\"%s\"> | |
<li>%s</li> | |
</a> | |
</div>", | |
'accordion-group', | |
'accordion-heading', | |
'#'.$args->menu_id, | |
$item->url, | |
$item->title, | |
$item->post_name, | |
$class_for_item | |
); | |
} | |
elseif ($depth === 1) { | |
// Setup accordion body classes | |
$accordion_body_classes = 'accordion-body collapse'; | |
$accordion_body_classes .= ($item->current) ? ' in':''; | |
// Get parent menu item | |
$parent_item_id = get_post_meta( $item->menu_item_parent, '_menu_item_object_id', true ); | |
$parent_item = get_category( $parent_item_id ); | |
// Manage Pages & Categories | |
if ($parent_item->slug !== 'customwalker') { | |
$parent_slug = $parent_item->slug; | |
} | |
else { | |
$parent_item = get_post( $parent_item_id ); | |
$parent_slug = $parent_item->post_name; | |
} | |
$output .= sprintf( "\n | |
<div id=\"%s\" class=\"%s\"> | |
<div class=\"%s\"> | |
<li> | |
<a href=\"%s\"> | |
%s | |
</a> | |
</li> | |
</div>", | |
$parent_slug, | |
$accordion_body_classes, | |
'accordion-inner', | |
$item->url, | |
$item->title | |
); | |
} | |
} | |
function end_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { | |
if ($depth === 0) { | |
$output .= sprintf( " | |
</div>\n"); | |
} | |
elseif ($depth === 1) { | |
$output .= sprintf( " | |
</div>"); | |
} | |
} | |
} |
This file contains hidden or 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 | |
$nav_args = array( | |
'container' => 'div', | |
'container_class' => 'side-nav-collapse', | |
'menu' => 2, //menu id | |
'items_wrap' => '<ul><div id="%1$s" class="%2$s">%3$s</div></ul>', | |
'menu_id' => 'accordion2', | |
'menu_class' => 'accordion', | |
'walker' => new Codeable_Walker_Menu() //use our custom walker | |
); | |
wp_nav_menu( $nav_args ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment