具有多个ul至wp菜单的静态站点菜单

时间:2017-05-01 作者:Sergi

我需要将此菜单从静态站点转换为具有wp_nav_menu() 功能,内部有2个ul,因此我不确定如何进行:

<div class="nav-menu"> 
                <ul class="nav-container">
                    <li class="list-item-main"> <span> <a href="#"> Sobre nosotros. </a> </span> </li>
                    <li class="list-item-main"> <span> <a href="#"> Real estate. </a> </span> 
                        <ul>
                            <li> <a href="#"> Oferta </a></li>
                            <li> <a href="#"> Venta </a> </li>
                            <li> <a href="#"> Colaboración </a> </li>
                        </ul>
                    </li>
                    <li class="list-item-main"> <span> <a href="#"> Consulting. </a></span> 
                        <ul>
                            <li> <a href="#"> Import/Export </a></li>
                        </ul>
                    </li>
                    <li class="list-item-main"> <span> <a href="#"> Contacto. </span> </a> </li>
                </ul>
            </div>  

1 个回复
最合适的回答,由SO网友:cjbj 整理而成

这个html 你给出的是标准的WordPress行为,甚至包括包装<div>. 此外,您还可以将参数传递给wp_nav_menu 更好地满足您的需求。以下内容应复制您的html:

$args = array(
    \'theme_location\'  => \'\',
    \'menu\'            => \'\',
    \'container\'       => \'div\',
    \'container_class\' => \'nav-menu\',
    \'container_id\'    => \'\',
    \'menu_class\'      => \'nav-container\',
    \'menu_id\'         => \'\',
    \'echo\'            => true,
    \'fallback_cb\'     => \'wp_page_menu\',
    \'before\'          => \'\',
    \'after\'           => \'\',
    \'link_before\'     => \'\',
    \'link_after\'      => \'\',
    \'items_wrap\'      => \'<ul id=\\"%1$s\\" class=\\"%2$s\\">%3$s</ul>\',
    \'depth\'           => 0,
    \'walker\'          => \'\');

wp_nav_menu ($args);
items_wrap 您看到的默认参数<ul> 环绕(子)菜单的。如果你想让它与众不同,那是可能的。

唯一不标准的是应用于<li> 项目。幸运的是there is a filter 为此:

add_filter (\'nav_menu_css_class\',\'wpse265503_nav_li_class\',10,4);

function wpse265503_nav_li_class ($classes,$item,$args,$depth){
    if ($depth==0) // only do this on the top items
      $classes[] = "list-item-main";
    return $classes;
    }