How Do I Add a Class to First / Top Level Links in Autonav?

Permalink 1 user found helpful
I have a jquery menu script that requires that I add a class to the first (TOP) level links within the menu. I am customizing the autonav for a theme.

I know that I have to edit the file within theme: /mytheme/blocks/autonav/templates/custom_menu.php

How do I add a class to the top-level links?

PineCreativeLabs
 
jordanlev replied on at Permalink Best Answer Reply
jordanlev
The built-in autonav template is a beast to modify because the code is so messy. I've created a much cleaner template that I use as a starting point for customizations -- it is functionally the exact same thing as the built-in autonav block but the code is cleaner. If you've already customized the autonav template a lot, it might be too much trouble, but if this is the only change you need to make, I think this will be much easier.

Here is the template:
https://raw.github.com/jordanlev/c5_clean_block_templates/master/aut...

Copy all of that and paste it into your custom_menu.php file (overwriting everything you already have there). Then, search for "if ($ni->is_first)" (about 3/4 the way down the page). Remove the comments around this chunk of code -- so you're changing this:
/*
if ($ni->is_first) {
   //class for the first item in each menu section (first top-level item, and first item of each dropdown sub-menu)
   $classes[] = 'nav-first';
}
*/

...to this:
if ($ni->is_first) {
   //class for the first item in each menu section (first top-level item, and first item of each dropdown sub-menu)
   $classes[] = 'nav-first';
}

(you're not really changing anything, just deleting the "/*" line and the "*/" line).

Now your autonav menu will have the nav-first class on the first item in each level.

If you only want the "nav-first" class on the first item of a dropdown menu, change this line:
if ($ni->is_first) {

...to this:
if ($ni->is_first && $ni->level > 1) {


If you want a different class name, change this line:
$classes[] = 'nav-first';

...to this:
$classes[] = 'whatever-class-you-want';