Add class to the link before a sub-menu, not the list item before it (2 posts)

  1. ferdiaob
    Posted 3 years ago #

    I am looking to do almost exactly what this function does in WordPress:

    `add_filter('wp_nav_menu_objects', function ($items) {
    $hasSub = function ($menu_item_id, &$items) {
    foreach ($items as $item) {
    if ($item->menu_item_parent && $item->menu_item_parent==$menu_item_id) {
    return true;
    return false;

    foreach ($items as &$item) {
    if ($hasSub($item->ID, &$items)) {
    $item->classes[] = 'menu-parent-item'; // all elements of field "classes" of a menu item get join together and render to class attribute of <li> element in HTML
    return $items;

    This handy function attaches a class "menu-parent-item" to the li that contains a sub-menu. What I would like to do is add that same class (or any other usable class of course) to the link beforehand instead. In other words I would end up with this:

    <a href="index.php" class="menu-parent-item">
    <ul class="sub-menu">

    And so forth. Any ideas?

  2. ferdiaob
    Posted 3 years ago #

    I missed the hacks subforum rule. Could a moderator move this please?

Topic Closed

This topic has been closed to new replies.

About this Topic