• I am trying to edit the navigation menu and trying wrap anchor tag over an div.I am trying to achieve the following result:

    <div class="nav-drawer-top-links">
        <a class="nav-content-column vertical-content-center" href="https://localhost/test/portfolio/">
    
        <div class="nav-content-box">
            Portfolio
            <span class="sub">Whatever</span>
        </div>
     </a>
    </div>

    But the result I keep getting is the anchor tag repeating and one of it closing outside of the div. Another anchor tag is being printed inside the div. I keep getting this result:

    <div class="nav-drawer-top-links">
        <a class="nav-content-column vertical-content-center" href="https://localhost/test/portfolio/"></a>
    
        <div class="nav-content-box">
            <a class="nav-content-column vertical-content-center" href="https://localhost/test/portfolio/">Portfolio<br>
            <span class="sub">Whatever</span></a>
        </div>
    </div>

    This the code in my header.php

    <?php $walker = new Menu_With_Description; ?>
                    <div class="nav-drawer-wrap">
                    <?php
                        wp_nav_menu( array(
    
                            'theme_location'    => 'primary',
                            'container'         => 'div',
                            'container_class'   => 'nav-drawer-interior',
                            'menu_class'        => 'nav-drawer-top-links',
                            'items_wrap'      => '<div class="nav-drawer-top-links">%3$s</div>',
                            'walker'            => $walker
    
                        ) );
                    ?>

    The following is my code functions.php

    class Menu_With_Description extends Walker_Nav_Menu {
    function start_el(&$output, $item, $depth, $args) {
        global $wp_query;
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    
        $class_names = $value = '';
    
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
    
        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
        $class_names = ' class="' . esc_attr( $class_names ) . '"';
    
        $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
        $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
        $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
    
        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'class="nav-content-column vertical-content-center"><div class="nav-content-box">';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= '<br /><span class="sub">' . $item->description . '</span>';
        $item_output .= '</a></div>';
        $item_output .= $args->after;
    
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • What are the actual items in your menu?

    Based on the output, it looks like there are two items in your menu, each of which is getting wrapped in the nav-content-column vertical-content-center tag.

    Are you trying to wrap the whole menu in the nav-content-column vertical-content-center tag?

    To debug this, you can try to output $args->before to the debug.log file so you can see more about what’s happening in the start_el function.

    Thread Starter Stephen Paul Samynathan

    (@stephen1204paul)

    TO make it more clear this is the result I am trying to achieve:

    <div class="nav-drawer-top-links">
        <a class="nav-content-column vertical-content-center" href="https://localhost/test/portfolio/">
    
        <div class="nav-content-box">
            Portfolio
            <span class="sub">Whatever</span>
        </div>
     </a>
    <a class="nav-content-column vertical-content-center" href="https://localhost/test/portfolio/">
    
        <div class="nav-content-box">
            Portfolio
            <span class="sub">Whatever</span>
        </div>
     </a>
    <a class="nav-content-column vertical-content-center" href="https://localhost/test/portfolio/">
    
        <div class="nav-content-box">
            Portfolio
            <span class="sub">Whatever</span>
        </div>
     </a>
    </div>

    But the result that I keep getting is:

    <div class="nav-drawer-top-links">
        <a class="nav-content-column vertical-content-center" href="https://localhost/test/portfolio/"></a>
    
        <div class="nav-content-box">
            <a class="nav-content-column vertical-content-center" href="https://localhost/test/portfolio/">Portfolio<br>
            <span class="sub">Whatever</span></a>
        </div>
    <a class="nav-content-column vertical-content-center" href="https://localhost/test/portfolio/"></a>
    
        <div class="nav-content-box">
            <a class="nav-content-column vertical-content-center" href="https://localhost/test/portfolio/">Portfolio<br>
            <span class="sub">Whatever</span></a>
        </div>
    <a class="nav-content-column vertical-content-center" href="https://localhost/test/portfolio/"></a>
    
        <div class="nav-content-box">
            <a class="nav-content-column vertical-content-center" href="https://localhost/test/portfolio/">Portfolio<br>
            <span class="sub">Whatever</span></a>
        </div>
    </div>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WordPress Wrapping tag within another tag in Navigation Menu’ is closed to new replies.