I would like to set up a custom menu walker to remove the unordered list and wrap each individual link in a custom div element. The desired output would look like this:

<div class="col-md-3"><a href="https://wordpress.stackexchange.com/questions/248369/linkurl">Link Title</a></div>

This is what I have tried so far:

<? wp_nav_menu( array( 'menu' => 'Main', 'container' => '', 'items_wrap' => '%3$s', 'link_before' => '<div class="col-md-3">', 'link_after' => '</div>' ) ); ?>

However the above code is producing this output:

<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-47">
    <a href="https://wordpress.stackexchange.com/questions/248369/linkurl"><div class="col-md-3">Link Title</div></a>
</li>

Here is my updated attempt based on the resource shared by @shahar:

<?php wp_nav_menu( array('menu' => 'Main', 'walker' => new Footer_Walker()) ); ?>

And in my functions.php:

class Footer_Walker extends Walker_Nav_Menu {
  function start_el(&$output, $item, $depth=0, $args=array()) {
    $output .= "<div>".esc_attr($item->label);
  }

  function end_el(&$output, $item, $depth=0, $args=array()) {
    $output .= "</div>\n";
  }
}

Using the code above the menu is not displaying at all.


Update: Here is the final code I ended up using in my functions file:

class Footer_Walker extends Walker_Nav_Menu {
  function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
    $classes = empty($item->classes) ? array () : (array) $item->classes;
    $class_names = join(' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
    !empty ( $class_names ) and $class_names=" class="". esc_attr( $class_names ) . '"';
    $output .= "<div class="col-sm-6 col-md-3">";
    $attributes="";
    !empty( $item->attr_title ) and $attributes .= ' title="'  . esc_attr( $item->attr_title ) .'"';
    !empty( $item->target ) and $attributes .= ' target="' . esc_attr( $item->target     ) .'"';
    !empty( $item->xfn ) and $attributes .= ' rel="'    . esc_attr( $item->xfn        ) .'"';
    !empty( $item->url ) and $attributes .= ' href="'   . esc_attr( $item->url        ) .'"';
    $title = apply_filters( 'the_title', $item->title, $item->ID );
    $item_output = $args->before
    . "<a $attributes>"
    . $args->link_before
    . $title
    . '</a></div>'
    . $args->link_after
    . $args->after;
    $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  }
}

And here’s the code to generate the menu in your template:

<?php wp_nav_menu(array('menu' => 'Footer', 'items_wrap'=> '%3$s', 'walker' => new Footer_Walker(), 'container'=>false, 'menu_class' => '', 'theme_location'=>'footer', 'fallback_cb'=>false )); ?>

1
1

You need to set-up your own menu walker (link to WordPress Codex), and in there add custom start_el and end_el overrides. So for example your start_el and end_el may look something like this:

function start_el(&$output, $item, $depth=0, $args=array()) {
    $output .= "<div>" . esc_attr($item->label);
}

function end_el(&$output, $item, $depth=0, $args=array()) {
    $output .= "</div>\n";
}

This tutorial might be useful too:
https://code.tutsplus.com/tutorials/understanding-the-walker-class–wp-25401

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *