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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化