Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Mt_Walker_Nav_Menu_Dropdown extends Walker_Nav_Menu {
- private $closed = true;
- function start_lvl(&$output, $depth) {
- $indent = str_repeat("\t", $depth); // don't output children opening tag (`<ul>`)
- }
- function end_lvl(&$output, $depth) {
- $indent = str_repeat("\t", $depth); // don't output children closing tag
- }
- function start_el(&$output, $item, $depth, $args) {
- if( !$this->closed ) {
- $output .= "</option>\n";
- $this->closed = true;
- }
- // add spacing to the title based on the depth
- $item->title = str_repeat(" ", $depth * 4) . $item->title;
- parent::start_el(&$output, $item, $depth, $args);
- // no point redefining this method too, we just replace the li tag...
- $output = str_replace('<li', '<option', $output);
- $output = str_replace('</a>', '', $output);
- $output = preg_replace('/><a href="(.*?)">/', ' value="\\1">', $output);
- $output = preg_replace('/class="(.*?) current-menu-item(.*?)"/', 'selected="selected"', $output);
- $output = preg_replace('/ class="(.*?)"/', '', $output);
- $this->closed = false;
- }
- function end_el(&$output, $item, $depth) {
- }
- }
- // usage
- wp_nav_menu(array(
- 'container_class' => 'mt-responsive-menu',
- 'theme_location' => 'primary',
- 'walker' => new Mt_Walker_Nav_Menu_Dropdown(),
- 'items_wrap' => '<select><option value="#">'. __('Navigate to...', 'mt') .'</option>%3$s</option></select>'
- ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement