WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. ferdiaob
    Member
    Posted 1 year 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:

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

    And so forth. Any ideas?

  2. ferdiaob
    Member
    Posted 1 year 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