【问题标题】:How to add class for second and third level <li>如何为第二级和第三级添加课程 <li>
【发布时间】:2023-01-07 00:22:26
【问题描述】:

我在菜单中有 3 个级别:顶级 - 第二级 - 第三级。对于第一级,我用这段代码添加了类:

//This function is responsible for adding class to parent menu item's
function add_menu_parent_class($items)
{
    $parents = array();
    foreach ($items as $item) {
        //Check if the item is a parent item
        if ($item->menu_item_parent && $item->menu_item_parent > 0) {
            $parents[] = $item->menu_item_parent;
        }
    }

    foreach ($items as $item) {
        if (in_array($item->ID, $parents)) {
            //Add class to parents
            $item->classes[] = 'menu-item__parent';
        }
    }

    return $items;
}

add_filter('wp_nav_menu_objects', 'add_menu_parent_class');

现在我需要为第二级添加一个单独的类和第三级的单独课程.我怎样才能做到这一点?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以轻松检查参数是否有孩子,然后为这些孩子添加您的课程

    if ( $args->has_children ) {
        //trait to add class 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      相关资源
      最近更新 更多