基于类向子菜单添加标记

时间:2021-09-10 作者:imz

I would like to add a div at the after a sub menu. I would like to apply this change only if the parent menu item has a class name of product-menu (comes from CSS class field under menus section in WP Admin).

This is the markup I want.

<li id="" class="product-menu menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-has-children">
  <a href="">Categories</a>
  <div class="sub-menu__wrapper">
    <ul class="sub-menu sub-menu-level-1">
      <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-has-children">
        <a href="">Vehicles</a>
        <ul class="sub-menu sub-menu-level-2">
          <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat"><a href="">Cars and Bikes</a></li>
          <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat"><a href="">Finance And Hire</a></li>
        </ul>
      </li>
      <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_catmenu-item-has-children">
        <a href="">Beauty</a>
        <ul class="sub-menu sub-menu-level-2">
          <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat"><a href="">Cosmetics</a></li>
          <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat"><a href="">Haircare</a></li>
          <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat"><a href="">Hygiene & Sanitary</a></li>
        </ul>
      </li>
    </ul>
    <div>SHOW ONCE</div>
  </div>
</li>

This is what I have so far.

class WP_Walker extends Walker_Nav_Menu {

    function start_lvl( &$output, $depth = 0, $args = array() ) {
            $indent = ( $depth > 0  ? str_repeat( "\\t", $depth ) : \'\' ); // code indent
            $display_depth = ( $depth + 1); // because it counts the first submenu as 0
            $classes = array(
                    \'sub-menu\',
                    \'sub-menu-level-\' . $display_depth
            );
            $class_names = implode( \' \', $classes );

            if ($display_depth == 1) {
                    $output .= "\\n" . $indent . \'<div class="sub-menu__wrapper"><ul class="\' . $class_names . \'">\' . "\\n";
            } else {
                    $output .= "\\n" . $indent . \'<ul class="\' . $class_names . \'">\' . "\\n";
            }
    }

    function end_lvl(&$output, $depth = 0, $args = array()) {
      if ($depth = 2){
        $output .= \'<div>SHOW ONCE</div></ul>\';
      } else {
        $output .= \'</ul>\';
      }
  }    
}

And what is currently being returned.

<li id="" class="product-menu menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-has-children">
  <a href="">Categories</a>
  <div class="sub-menu__wrapper">
    <ul class="sub-menu sub-menu-level-1">
      <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat menu-item-has-children">
        <a href="">Vehicles</a>
        <ul class="sub-menu sub-menu-level-2">
          <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat"><a href="">Cars and Bikes</a></li>
          <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat"><a href="">Finance And Hire</a></li>
          <div>SHOW ONCE</div>
        </ul>
      </li>
      <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_catmenu-item-has-children">
        <a href="">Beauty</a>
        <ul class="sub-menu sub-menu-level-2">
          <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat"><a href="">Cosmetics</a></li>
          <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat"><a href="">Haircare</a></li>
          <li id="" class="menu-item menu-item-type-taxonomy menu-item-object-product_cat"><a href="">Hygiene & Sanitary</a></li>
          <div>SHOW ONCE</div>
        </ul>
      </li>
      <div>SHOW ONCE</div>
    </ul>
  </div>
</li>

As you can see the div I\'m trying to add is getting added multiple times. How can I get the div to only appear once straight after the sub-menu-level-1 list?

Also how can I apply this change so it only applies for this particular menu with the class name of product-menu? Other sub menus don\'t require this additional div hence why I\'m trying to do it by class name.

1 个回复
SO网友:Rajneesh Tiwari

这是你的密码

<script>
 $(document).ready(function(){
 $( ".product-menu .sub-menu-level-1" ).after( "<div>SHOW ONCE</div>" );
 });
</script>
如果父级的类名为product menu,则会自动添加div。如果我错过了什么,请告诉我。如果这解决了你的问题,请伸出大拇指