Skip to main content
edited body
Source Link
Krzysiek Dróżdż
  • 25.5k
  • 9
  • 53
  • 74

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 wordpressWordPress:

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

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

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

Source Link
Rozo
  • 25
  • 3

Walker_Nav_Menu: Items not nested correctly

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