Home / WordPress Menu Functions

WordPress Menu Functions

Register a new menu

// Start of Register menu 
function menu_registration_prism_10928(){
    register_nav_menus(array(
        "primary_menu" => __('Primary Menu', 'prism')
        ));
    register_nav_menus(array(
        "menu_footer" => __('Footer Menu', 'prism')
        ));
}
add_action('init', 'menu_registration_prism_10928');
// End of Register menu

Add extra classes on ul>li

// Start of set class on ul>li
function add_menu_classes_prism_10928($classes, $item, $args) {
    if($args->theme_location == 'primary_menu') {
        $classes[] = 'nav-item';
    }
    return $classes;
}
add_filter('nav_menu_css_class', 'add_menu_classes_prism_10928', 1, 3);
// End of set class on ul>li

Add extra classes on ul>li>a

// Start of set class on ul>li-a
function menu_a_add_class_prism_10928( $atts, $item, $args ) {
    if($args->theme_location == 'primary_menu') {
        $class = 'nav-link';
        $atts['class'] = $class;
    }
    return $atts;
}
add_filter( 'nav_menu_link_attributes', 'menu_a_add_class_prism_10928', 10, 3 );
// End of set class on ul>li-a

Show your menu now!

Full documentation here

wp_nav_menu( array(
    'theme_location'  => 'primary_menu',
    'menu_class'      => 'navbar-nav ml-auto main_primary_menu',
    'menu_id'         => '',
    'container_class' => 'collapse navbar-collapse',
    'container_id'    => 'navbarSupportedContent',
));

all rights reserved