16

I'm trying to add the standard Bootstrap nav-link class to the anchors rendered by the WordPress menu.

  1. I can pass variables to the wp_nav_menu() which applies a class to the menu (and remove the containing):

    <?php wp_nav_menu(array(
        'theme_location' => 'header-menu',
        'menu_class' => 'navbar-nav',
        'container' => 'false'
    ) );
    ?>
    
  2. I then use the WordPress Appearances > Menu UI to apply nav-item class to <li> tags.

How do I apply a class to the WordPress menu anchor tags?

Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
TimothyAURA
  • 1,329
  • 5
  • 21
  • 44
  • To avoid adding classes in the menu UI you can use the 'nav_menu_css_class'. I'll add this to my answer... – csknk Mar 24 '17 at 10:09

5 Answers5

49

You can do what you need with the WP nav_menu_link_attributes hook:

add_filter( 'nav_menu_link_attributes', function($atts) {
        $atts['class'] = "nav-link";
        return $atts;
}, 100, 1 );

...or if you don't like closures:

function add_link_atts($atts) {
  $atts['class'] = "nav-link";
  return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_link_atts');

Filter menu list items

To avoid using the WordPress menu UI to add classes to the menu list items, you can take advantage of the 'nav_menu_css_class' filter:

add_filter( 'nav_menu_css_class', function($classes) {
    $classes[] = 'nav-item';
    return $classes;
}, 10, 1 );
csknk
  • 1,899
  • 1
  • 16
  • 27
14

If you have multiple menus over your site or just want to be flexible. You can extend the wp_nav_menu build-in function:

Just add add_a_class to your wp_nav_menu function:

wp_nav_menu(
    array(
        'theme_location'  => 'primary',
        'depth'           => 2,
        'container'       => 'div',
        'add_a_class'     => 'class1 class2',
        'fallback_cb'     => false,
    )
);

Add the following code in your functions.php:

function add_additional_class_on_a($classes, $item, $args)
{
    if (isset($args->add_a_class)) {
        $classes['class'] = $args->add_a_class;
    }
    return $classes;
}

add_filter('nav_menu_link_attributes', 'add_additional_class_on_a', 1, 3);
Lars Flieger
  • 2,421
  • 1
  • 12
  • 34
2

This allows you to add classes ONLY to anchors of SPECIFIC MENU. Just add your classes into $menuClassMap values below.

    function mytheme__menu_anchors_add_css( $atts, $item, $args ) {

        $menuClassMap = [
            'navbar-menu' => 'my-footer-menu-link-class',
            'footer-menu' => 'my-footer-menu-link-class',
        ];

        $additionalClassName = $menuClassMap[$args->menu->slug] ?? '';

        if($additionalClassName){
            if(!array_key_exists('class', $atts)){
                $atts['class'] = '';
            }
            $classList = explode (' ' , $atts['class']);
            $classList[] = $additionalClassName;
            $atts['class'] = implode (' ' , $classList);
        }
        return $atts;

    }

    add_filter( 'nav_menu_link_attributes', 'mytheme__menu_anchors_add_css', 10, 3 );
Fusion
  • 5,046
  • 5
  • 42
  • 51
0

wp_nav_menu()'s parameters wont allow you to add a class to your links with default's functionality. It would allow you to enclose your <a href="#"></a> within any html like <span class="wrapped-anchor"><a href="#"></a></span> if you use :

<?php 
   $parameters = TimothyAURA->set_specific_parameters_you_want(); // i mean, realy fullfill the array with the options you want based on the docs.
   $parameters['before'] = '<span class="wrapped-anchor">';
   $parameters['after'] = '</span>';
   wp_nav_menu($parameters);

In case you really need to set a class for your anchors you would have to pass a Walker object. You would need to read and understand this specific docs about it, though.

Ruben Marrero
  • 1,392
  • 1
  • 10
  • 23
0

If you want to add a class in anchor tag for a specific menu then can use following steps

Step-1: add this in functions.php

function add_class_on_a_tag($classes, $item, $args)
{
    if (isset($args->add_a_class)) {
        $classes['class'] = $args->add_a_class;
    }
return $classes;
}

add_filter('nav_menu_link_attributes', 'add_class_on_a_tag', 1, 3);

Step-2:- use it like this in your theme

<?php
    // Show Menu here
    wp_nav_menu(array(
        'theme_location' => 'my-menu',
        'menu_class'      => 'footer-top',
        'add_a_class'     => 'nav-link',
    ));
?>
Sachin Saini
  • 359
  • 2
  • 10