WordPress : custom menu, add class "current-menu-item" on <a> tags

I use a CSS framework and a custom menu for wordpress :

        $menuParameters = array(
            'theme_location' => 'menu-1',
            'menu_id' => 'primary-menu',
            'container'       => false,
            'echo'            => false,
            'items_wrap'      => '%3$s',
            'depth'           => 0,

        $output = strip_tags(wp_nav_menu($menuParameters),'<a>');
        $output = preg_replace('/<a/', '<a class="nav-item"', $output);
        echo $output;       

Result :

    <div class="nav-right nav-menu">
<a class="nav-item" href="/">Menu1</a>
<a class="nav-item" href="/"> Menu2</a>
<a class="nav-item" href="/"> Menu13</a>

I don’t find hook, walker_menu, or an other solution to add a class on the current menu (like “current-menu-item on the original WordPress menu)

Any idea ?

Thank you !

Read more here: WordPress : custom menu, add class "current-menu-item" on <a> tags

Leave a Reply

Your email address will not be published. Required fields are marked *