Skip to content

Instantly share code, notes, and snippets.

@salehahmadbabu
Created February 28, 2022 12:09
Show Gist options
  • Save salehahmadbabu/226ed3291175882401d50ff2d17b23f1 to your computer and use it in GitHub Desktop.
Save salehahmadbabu/226ed3291175882401d50ff2d17b23f1 to your computer and use it in GitHub Desktop.
<?php
// collection of walker classes
class Walker_Mobile_Menu extends Walker_Nav_Menu
{
function start_lvl(&$output, $depth = 0, $args = null)
{
$output .= "\n<ul class=\"dropdown-menu\">\n";
}
function start_el(&$output, $item, $depth = 0, $args = null, $id = 0)
{
$classnames = '';
$classes = empty($item->classes) ? '' : (array) $item->classes;
$classes[] = ($args->walker->has_children) ? 'dropdown' : '';
$classnames = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
$classnames = ' class="' . esc_attr($classnames) . '"';
$id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args);
$id = strlen($id) ? ' id="' . esc_attr($id) . '"' : '';
$output .= '<li' . $id . $classnames . '>';
$attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : '';
$item_output = $args->before;
$item_output .= '<a' . $attributes . '>';
$item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
}
class Walker_Desktop_Menu extends Walker_Nav_Menu
{
function start_lvl(&$output, $depth = 0, $args = null)
{
if ($depth == 0) {
$output .= "\n<ul class=\"dropdown\">\n";
} else {
$output .= "\n<ul class=\"dropdown2\">\n";
}
}
function start_el(&$output, $item, $depth = 0, $args = null, $id = 0)
{
$classnames = '';
$classes = empty($item->classes) ? '' : (array) $item->classes;
$classnames = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
$classnames = ' class="' . esc_attr($classnames) . '"';
$id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args);
$id = strlen($id) ? ' id="' . esc_attr($id) . '"' : '';
$output .= '<li' . $id . $classnames . '>';
$attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : '';
$item_output = $args->before;
$item_output .= '<a' . $attributes . '>';
$item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
$item_output .= ($depth == 0 && $args->walker->has_children) ? '<span class="toggle"></span>' : '';
$item_output .= ($depth > 0 && $args->walker->has_children) ? '<span class="toggle2"></span>' : '';
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment