I found a solution at this site through the use of a custom walker.
Two steps: replace the default wp_nav_menu code with an edited one, and then add code to the functions.php of the theme.
First, replace the default wp_nav_code with the following (the code is copied from the above site):
wp_nav_menu( array(
'menu' => 'Main Menu',
'container' => false,
'menu_class' => 'nav',
'echo' => true,
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => new description_walker())
);
Next, add the following code to functions.php. By doing this you can actually add a class to the menu links:
class description_walker extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args)
{
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' class="'. esc_attr( $class_names ) . '"';
$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$prepend = '<strong>';
$append = '</strong>';
$description = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';
if($depth != 0)
{
$description = $append = $prepend = "";
}
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
$item_output .= $description.$args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
if ($item->menu_order == 1) {
$classes[] = 'first';
}
}
}
Towards the end of the code are several lines that start with $item_output. In particular, you want to look at this piece:
$item_output .= '<a'. $attributes .'>';
Because this line determines the output for the beginning of the link html. If you change it to something like this:
$item_output .= '<a'. $attributes . 'class="abc"' .'>';
Then all your links in the menu will have class="abc" added to them.
That said, it doesn't allow a custom class for each link (or at least I don't know how to code it). This is an issue for me.
For those asking why would you want to do this? I want to have my menu links open lightboxes (colorboxes, to be more specific), and they require classes on the links to do that. For example:
<a class="lightbox1" href="#">Photo</a>
Is there possibly a way to dynamically generate the classes, such as "lightbox1" for the first link, "lightbox2" for the second link, and so on?
.class
to.class a
?<li>
element. If you have a submenu below that particular item it's no problem, you can tackle that in the CSS to(i can give examples if necessary).