duan0417 2017-06-06 18:10
浏览 37
已采纳

在WordPress子菜单项后添加div

I am trying to add a div after any submenu items in a WordPress menu. I am working on the following code in the functions.php file:

add_filter( 'nav_menu_link_attributes', 'SubMenuCheck', 10, 3 );
function SubMenuCheck( $atts, $item, $args ) {
  if (in_array('menu-item-has-children', $item->classes)) {
    $args->after = '<div class="click">+</div>';
  }
  return $atts;
}

The code above adds the new div <div class="click">+</div>but keeps applying it to all parent and child items after the first 'menu-item-has-children' class:

menu issue example

Does anyone know why this is repeating?... I only want the div to display for items with the menu-item-has-children class.

Thanks

  • 写回答

1条回答 默认 最新

  • dua55014 2017-06-06 18:40
    关注

    I don't use the nav_menu_link_attributes filter, to add a button (which is semantically correct) to be the hot spot for toggling the children, I use the following:

    function prefix_add_button_after_menu_item_children( $item_output, $item, $depth, $args ) {
    
        if ( $args->theme_location == 'primary' ) {
    
            if ( in_array( 'menu-item-has-children', $item->classes ) || in_array( 'page_item_has_children', $item->classes ) ) {
                $item_output = str_replace( $args->link_after . '</a>', $args->link_after . '</a><button class="sub-menu-toggle" aria-expanded="false" aria-pressed="false"><span class="screen-reader-text">' . _x( 'open dropdown menu', 'verb: open the menu', 'text-domain' ) . '</span></button>', $item_output );
            }
        }
    
        return $item_output;
    }
    add_filter( 'walker_nav_menu_start_el', 'prefix_add_button_after_menu_item_children', 10, 4 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器