最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

walker - How to change the menu mark up with walker_nav_menu class?

programmeradmin1浏览0评论

I want to remove the < ul >,< li > tags and add a < span > tag inside the < a > tag.

    <nav class="menu">
       <div class="menu-list">
          <a data-scroll="" href="#" class="">
             <span>Home</span>
          </a>
       </div>
    </nav>

Any help or explanation is appreciated.

This is where I am right now

class Walker_Nav_primary extends Walker_Nav_Menu
{

    function start_lvl( &$output)
    {   // ul
        $indent = str_repeat( &output, $depth );

    }

    function start_el(argument)
    {   // anything inside ul - opening tags
        # code...
    }

    function end_el(argument)
    {   // anything inside ul - closing tags
    # code...
    }

    function end_lvl(argument)
    {   // close ul
    # code...
    }

}

I want to remove the < ul >,< li > tags and add a < span > tag inside the < a > tag.

    <nav class="menu">
       <div class="menu-list">
          <a data-scroll="" href="#" class="">
             <span>Home</span>
          </a>
       </div>
    </nav>

Any help or explanation is appreciated.

This is where I am right now

class Walker_Nav_primary extends Walker_Nav_Menu
{

    function start_lvl( &$output)
    {   // ul
        $indent = str_repeat( &output, $depth );

    }

    function start_el(argument)
    {   // anything inside ul - opening tags
        # code...
    }

    function end_el(argument)
    {   // anything inside ul - closing tags
    # code...
    }

    function end_lvl(argument)
    {   // close ul
    # code...
    }

}
Share Improve this question asked Dec 5, 2016 at 18:27 Mahade WalidMahade Walid 1451 silver badge7 bronze badges 1
  • Have a read through this: code.tutsplus/tutorials/… – montrealist Commented Dec 5, 2016 at 18:52
Add a comment  | 

1 Answer 1

Reset to default 1

I just posted an answer to this here: How to create this custom menu walker?

Basically, you want your start_el and end_el to looks something like this:

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

function end_el(&$output, $item, $depth=0, $args=array()) {
    $output .= '</span></a>';
}
发布评论

评论列表(0)

  1. 暂无评论