简体   繁体   English

我们可以通过覆盖 The Walker_Nav_menu 使用 PHP 更改 wordpress 自定义 walker 类的第一个(根)ul 标签吗

[英]Can we change wordpress custom walker class first (root) ul tag using PHP by overriding The Walker_Nav_menu

Hey guys using the Wordpress Framework usually I create a custom walker that extends Walker_Nav_menu in order to make a custom wordpress menu.嘿,使用 Wordpress 框架的人通常我会创建一个自定义 walker,它扩展Walker_Nav_menu以制作自定义 wordpress 菜单。 I did register the Menu location in the functions.php , I did hook up the menu and create a custom_nav_walker and it worked.我确实在 functions.php 中注册了 Menu 位置,我确实连接了菜单并创建了一个 custom_nav_walker 并且它起作用了。 however I cannot change the main ul root tag to put some inline code up there using the navwalker php class without using javascript.但是,我无法更改主 ul 根标记以在不使用 javascript 的情况下使用 navwalker php 类将一些内联代码放在那里

So the nav menu html is something like this.所以导航菜单html是这样的。

<ul id="Root-tag-That-I-want-to-modify" class="vertical medium-horizontal menu dropdown" <!--  I WANT TO ECHO CODE HERE USING PHP --> >
<li id="menu-item-146" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home active menu-item-146">
<a href="http://Thelink">Item</a></li>
<li id="menu-item-147" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-ancestor current-menu-parent current_page_parent current_page_ancestor menu-item-has-children active has-dropdown menu-item-147"><a href="http://ThesecondLink">submeu</a>
<ul class="0 menu submenu is-dropdown-submenu first-sub vertical"  data-submenu="" role="menu">
<li id="menu-item-153" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home active menu-item-153"><a href="http://anotherlink">submeu item</a> 
</li></ul></li></ul>        

I tried to override the start_lvl funtion that is inside the Walker class :我试图覆盖 Walker 类中的 start_lvl 函数:

    function start_lvl( &$output, $depth = 0, $args = array() ) {
      $indent = str_repeat( "\t", $depth );
      $first = ($depth === 0) ? 'first-sub' : '';
      $output .= "\n$indent<ul class=\"$depth menu submenu is-dropdown-submenu $first vertical\"  data-submenu=\"\" role=\"menu\">\n";
     }

After printing $depth inside the ul tag I found out that this first UL is not processed by this start_lvl在 ul 标签内打印 $depth 后,我发现第一个 UL 没有被这个 start_lvl 处理

Where in this class can I override the main UL tag, I checked the wordpress call wp_nav_menu array maybe I missed something ??我可以在这个类中的哪里覆盖主要的 UL 标签,我检查了 wordpress 调用 wp_nav_menu 数组,也许我错过了什么?? I am interested in doing this with PHP without Javascript.我有兴趣在没有 Javascript 的情况下使用 PHP 执行此操作。

One solution that I found is to change the 'menu_class' parameter when instantiating the walker class , Usually you have :我发现的一种解决方案是在实例化 walker 类时更改“menu_class”参数,通常您有:

wp_nav_menu( array(
        'menu_class'      => 'vertical medium-horizontal menu dropdown',
        'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
        'depth'           => 0,
        'walker'          => new Custom_Walker_Class()  ));

To include some extra line-codes example ( data-responsive-menu="accordion medium-dropdown" role="menubar' ) I changed it to :为了包含一些额外的行代码示例( data-responsive-menu="accordion medium-dropdown" role="menubar' ),我将其更改为:

wp_nav_menu( array(
        'menu_class'      => 'vertical medium-horizontal menu dropdown" data-responsive-menu="accordion medium-dropdown" role="menubar',
        'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
        'depth'           => 0,
        'walker'          => new Custom_Walker_Class()  ));

Basically it is shipping all the ul tag inline code inside this parameters string.基本上,它在此参数字符串中传送所有 ul 标记内联代码。 Since it is getting a string (which is supposed to be just the ul tag classes) and wrapping it in quotes to print it as a wrapper for nav elements in the Html.因为它正在获取一个字符串(它应该只是 ul 标记类)并将其包装在引号中以将其打印为 Html 中导航元素的包装器。

Ok So far the idea is good but I couldn't escape the quotes and I ended with some source code like this :好的,到目前为止,这个想法很好,但我无法逃避引号,我以这样的一些源代码结束:

class=&quot ;vertical medium-horizontal menu dropdown&quot ; class="垂直中-水平菜单下拉菜单"; data-responsive-menu=&quot ;数据响应菜单=”; accordion medium-dropdown" role=&quot ; menubar&quot ;手风琴 medium-dropdown" role=" menubar" ;

What I did to correct things is to :我所做的纠正事情是:

  1. I stored the wp_nav_menu result into some variable我将 wp_nav_menu 结果存储到某个变量中
  2. I encoded that variable into a string (the &quot ; will become ")我将该变量编码为一个字符串(“; 将变成”)
  3. I decoded and printed the resulting HTML我解码并打印了生成的 HTML

     $db = esc_attr('" '); $inlinecode= esc_attr('vertical medium-horizontal menu dropdown' . $db . ' data-responsive-menu=' . $db . ' accordion medium-dropdown' . $db . ' role=' . $db . ' menubar' . $db . '); $mainmenu = wp_nav_menu( array( 'menu_class' => $inlinecode, 'echo' => false, 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 'depth' => 0, 'walker' => new Walker_Primary() )); $men_encoded = esc_attr($mainmenu); echo html_entity_decode($men_encoded);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM