Walker_Nav_Menu: Items not nested correctly

Rozo 10/10/2018. 1 answers, 46 views
menus

I want to add "anchor" tag adjacent to every "list item" that has children.

this is my Walker_Nav_Menu class:

class Menu_Walker extends Walker_Nav_Menu {

function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

    $ident = str_repeat( "\t", $depth );

    if ( $args->walker->has_children ) {

        $classes = 'menu-item-has-children menu-item menu-item-' . $item->ID;

        // actual link
        $output .= "$ident<li class=\"$classes\"><a href=\"$item->url\">$item->title</a></li>\n";

        // added tag after
        $output .= "<a class=\"added\" href=\"#\">txt</a>";

    } else {

        $classes = 'menu-item menu-item-' . $item->ID;

        // actual link
        $output .= "$ident<li class=\"$classes\"><a href=\"$item->url\">$item->title</a></li>\n";
    }
}

This is the structure of the menu in WordPress:

enter image description here

But the output is not nested correctly as you can see in image below:

enter image description here

Please help me fix this problem

1 Answers


Krzysiek Dróżdż 10/10/2018.

You have this problem, because of your code ;)

function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

    $ident = str_repeat( "\t", $depth );

    if ( $args->walker->has_children ) {

        $classes = 'menu-item-has-children menu-item menu-item-' . $item->ID;

        // actual link
        // you SHOULDN'T close the li tag in here
        $output .= "$ident<li class=\"$classes\"><a href=\"$item->url\">$item->title</a></li>\n";

        // added tag after
        $output .= "<a class=\"added\" href=\"#\">txt</a>";

    } else {

        $classes = 'menu-item menu-item-' . $item->ID;

        // actual link
        // you SHOULDN'T close the li tag in here
        $output .= "$ident<li class=\"$classes\"><a href=\"$item->url\">$item->title</a></li>\n";
    }
}

In your start_el you close the li tag, so children can't be printed inside of it...


HighResolutionMusic.com - Download Hi-Res Songs

1 BLACKPINK

Kiss And Make Up flac

BLACKPINK. 2018. Writer: Soke;Kny Factory;Billboard;Chelcee Grimes;Teddy Park;Marc Vincent;Dua Lipa.
2 Martin Garrix

Waiting For Tomorrow flac

Martin Garrix. 2018. Writer: Pierce Fulton;Mike Shinoda;Martijn Garritsen;Brad Delson.
3 John Legend

Written In The Stars flac

John Legend. 2018. Writer: Kiana Brown;Santoy;Kevin White;Mike Woods;MZMC;The Heavy Group;Rice N' Peas.
4 Martin Garrix

Access flac

Martin Garrix. 2018. Writer: Martin Garrix.
5 Martin Garrix

Yottabyte flac

Martin Garrix. 2018. Writer: Martin Garrix.
6 Alan Walker

Diamond Heart flac

Alan Walker. 2018. Writer: Alan Walker;Sophia Somajo;Mood Melodies;James Njie;Thomas Troelsen;Kristoffer Haugan;Edvard Normann;Anders Froen;Gunnar Greve;Yann Bargain;Victor Verpillat;Fredrik Borch Olsen.
7 Cardi B

Taki Taki flac

Cardi B. 2018. Writer: Bava;Juan Vasquez;Vicente Saavedra;Jordan Thorpe;DJ Snake;Ozuna;Cardi B;Selena Gomez.
8 Bradley Cooper

Shallow flac

Bradley Cooper. 2018. Writer: Andrew Wyatt;Anthony Rossomando;Mark Ronson;Lady Gaga.
9 Post Malone

Sunflower flac

Post Malone. 2018. Writer: Louis Bell;Billy Walsh;Carter Lang;Swae Lee;Post Malone.
10 Dyro

Latency flac

Dyro. 2018. Writer: Martin Garrix;Dyro.
11 Lady Gaga

I'll Never Love Again flac

Lady Gaga. 2018. Writer: Benjamin Rice;Lady Gaga.
12 Rita Ora

Let You Love Me flac

Rita Ora. 2018. Writer: Rita Ora.
13 Zara Larsson

Ruin My Life flac

Zara Larsson. 2018. Writer: Delacey;Michael Pollack;Stefan Johnson;Jordan Johnson;Sermstyle;Jackson Foote.
14 Halsey

Without Me flac

Halsey. 2018. Writer: Halsey;Delacey;Louis Bell;Amy Allen;Justin Timberlake;Timbaland;Scott Storch.
15 ZAYN

Fingers flac

ZAYN. 2018. Writer: Zayn Malik;Alex Oriet;David Phelan.
16 Dewain Whitmore

Burn Out flac

Dewain Whitmore. 2018. Writer: Dewain Whitmore;Ilsey Juber;Emilio Behr;Martijn Garritsen.
17 Mako

Rise flac

Mako. 2018. Writer: Riot Music Team;Mako;Justin Tranter.
18 Bradley Cooper

Always Remember Us This Way flac

Bradley Cooper. 2018. Writer: Lady Gaga;Dave Cobb.
19 Blinders

Breach (Walk Alone) flac

Blinders. 2018. Writer: Dewain Whitmore;Ilsey Juber;Blinders;Martin Garrix.
20 Julia Michaels

There's No Way flac

Julia Michaels. 2018. Writer: Ian Kirkpatrick;Justin Tranter;Julia Michaels;Lauv.

Related questions

Hot questions

Language

Popular Tags