I’m building a theme with Fundation and need to make some addition to the mobile menu, and would need to print a the name of the parent in the start_lvl function of the custom menu walker.
Is this possible?
Thanks in advance
]]>Also, I’ve tried wp_nav_menu with some arguments to control the wrapper (without the walker) to no avail:
$menu_args = array(
'menu' => '',
'container' => "ul",
'container_class' => 'main-nav-list container_class nav-collapse',
'menu_class' => 'main-nav-list menu_class nav-collapse',
'fallback_cb' => false,
'items_wrap' => '<ul id="%1$s" class="main-nav-list items_wrap nav-collapse">%3$s</ul>',
'theme_location' => 'primary'
);
It’s like something else is controlling the container / wrapper of the
What am I missing here? I’m going mad over this.
Cheers for your help!
If you look at function start_lvl this is the start of the sub menu ul tag. I need to add custom html on that but only on active pages. Is this possible?
class Roots_Nav_Walker extends Walker_Nav_Menu {
function check_current($classes) {
return preg_match('/(current[-_])|active|dropdown/', $classes);
}
function start_lvl(&$output, $depth = 0, $args = array()) {
$output .= "\n<ul class=\"sub-nav\">\n";
}
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
$item_html = '';
parent::start_el($item_html, $item, $depth, $args);
if ($item->is_dropdown && ($depth === 0)) {
$item_html = str_replace('<a', '<a class="dropdown-toggle" data-target="#"', $item_html);
$item_html = str_replace('</a>', '</a>', $item_html);
}
elseif (stristr($item_html, 'li class="divider')) {
$item_html = preg_replace('/<a[^>]*>.*?<\/a>/iU', '', $item_html);
}
elseif (stristr($item_html, 'li class="dropdown-header')) {
$item_html = preg_replace('/<a[^>]*>(.*)<\/a>/iU', '$1', $item_html);
}
$item_html = apply_filters('roots_wp_nav_menu_item', $item_html);
$output .= $item_html;
}
function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
$element->is_dropdown = ((!empty($children_elements[$element->ID]) && (($depth + 1) < $max_depth || ($max_depth === 0))));
if ($element->is_dropdown) {
$element->classes[] = 'dropdown';
}
parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
}
/**
* Remove the id="" on nav menu items
* Return 'menu-slug' for nav menu classes
*/
function roots_nav_menu_css_class($classes, $item) {
$slug = sanitize_title($item->title);
$classes = preg_replace('/(current(-menu-|[-_]page[-_])(item|parent|ancestor))/', 'active', $classes);
$classes = preg_replace('/^((menu|page)[-_\w+]+)+/', '', $classes);
$classes[] = 'menu-' . $slug;
$classes = array_unique($classes);
return array_filter($classes, 'is_element_empty');
}
add_filter('nav_menu_css_class', 'roots_nav_menu_css_class', 10, 2);
add_filter('nav_menu_item_id', '__return_null');
/**
* Clean up wp_nav_menu_args
*
* Remove the container
* Use Roots_Nav_Walker() by default
*/
function roots_nav_menu_args($args = '') {
$roots_nav_menu_args['container'] = false;
if (!$args['items_wrap']) {
$roots_nav_menu_args['items_wrap'] = '<ul class="%2$s">%3$s</ul>';
}
if (current_theme_supports('bootstrap-top-navbar') && !$args['depth']) {
$roots_nav_menu_args['depth'] = 2;
}
if (!$args['walker']) {
$roots_nav_menu_args['walker'] = new Roots_Nav_Walker();
}
return array_merge($args, $roots_nav_menu_args);
}
add_filter('wp_nav_menu_args', 'roots_nav_menu_args');
]]>I am trying to make the Twitter Bootstrap accordion menu to work in WordPress. Therefore I need to get the page ID in my walker function start_lvl to put it in the <ul>
tag so it can get triggered.
This is what I am trying to achieve in HTML:
<li id="" class="accordion-group">
<a href="#collapsePAGEID" class="active" data-toggle="collapse" data-parent="#menu">Menu Option 1</a>
<ul id="collapsePAGEID" class="accordion-body in collapse">
<li id="" class="active">
<a href="" class="">Submenu Option 1</a>
etc...
</li>
</ul>
</li>
I used start_el to put the page ID in <a href="#collapsePAGEID">
. But I can’t use start_lvl in the same way.
My code so far for start_lvl:
// add classes to ul sub-menus
function start_lvl( &$output, $depth, $item ) {
// depth dependent classes
$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent
$display_depth = ( $depth + 1); // because it counts the first submenu as 0
$pgid = ; //How to get the page ID?
$ids = array(
'collapse' . $pgid
);
$id_name = implode( ' ', $ids );
// build html
$output .= "\n" . $indent . '<ul id="' . $id_name . '" >' . "\n";
}
Any clues?? Much appreciated!
]]>I need to add unique identifiers to every level of the wp_list_categories list tree.
In situations with child categories, they are always listed as
<ul class="children">
I would like to change the start_lvl function in the Walker Class (Walker_Category) from:
function start_lvl(&$output, $depth, $args) {
if ( 'list' != $args['style'] )
return;
$indent = str_repeat("\t", $depth);
$output .= "$indent<ul class='children'>\n";
}
To:
function start_lvl(&$output, $depth, $args) {
if ( 'list' != $args['style'] )
return;
$indent = str_repeat("\t", $depth);
$output .= "$indent<ul id='children-$UNIQUE_ID' class='children'>\n";
}
This can be a category ID, or something similar, but each UL needs to have a fully unique ID attached to it.
The only variable I can get in there to parse is $depth, but that created non-unique IDs if there are multiple top-level cats, etc.
Any ideas?
]]>