1

I want to add a menu only using <div> and <a> to my Wordpress theme but I don't know how to do it.

When I put this code inside to head__menu class, it generate different menu with <ul> and <li>.

<?php wp_nav_menu(array('theme_location' => 'menu')); ?>

Can anyone help me? My navbar code is on below

<div class="container-fluid header">
<div class="row">
    <div class="col-12">
        <div class="navbar">
            <a href="<?php bloginfo('url'); ?>">
                <img src="<?php bloginfo('template_url'); ?>/img/logo.png" class="logo">
            </a>
            <label for="toggle">
                <i class="fas fa-bars"></i>
            </label>
            <input type="checkbox" id="toggle">
            <div class="head__menu">
                <a href="#">Home</a>
                <a href="#">Tech</a>
                <a href="#">Game</a>
            </div>
        </div>
    </div>
    <div class="cool-line"></div>
</div>

0

Below is a walker class that will do just that. Add this to your functions.php file;

class Description_Walker extends Walker_Nav_Menu
{
    function start_el(&$output, $item, $depth, $args)
    {
        $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 .= "";
        $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 $class_names>"
        . $args->link_before
        . $title
        . '</a>'
        . $args->link_after
        . $args->after;
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}



function header_nav()
{
    wp_nav_menu(
    array(
        'theme_location'  => 'header-menu',
        'menu'            => '',
        'container'       => '',
        'container_class' => '',
        'container_id'    => '',
        'menu_class'      => '',
        'menu_id'         => '',
        'echo'            => true,
        'fallback_cb'     => 'wp_page_menu',
        'before'          => '',
        'after'           => '',
        'link_before'     => '',
        'link_after'      => '',
                'items_wrap'      => '%3$s',        
                'depth'           => 0,
        'walker'          => new Description_Walker
        )
    );
}

Then finally, in your theme header add a call to the nav header_nav() wherever you want your menu to appear. Here I have wrapped it with a NAV element;

  <nav class="" id="" role="navigation">
      <?php header_nav(); ?>
  </nav>

This should output this;

  <nav role="navigation" id="">
     <a class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item" href="#">Link 1</a>
     <a class="menu-item menu-item-type-taxonomy menu-item-object-category" href="#">link 2</a>
     <a class="menu-item menu-item-type-taxonomy menu-item-object-category" href="#">link 3</a>
  </nav>

Source: https://stackoverflow.com/questions/12159486/wordpress-change-header-navigation-list-items-to-div/12161295#12161295

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.