Search
 
SCRIPT & CODE EXAMPLE
 

PHP

wordpress add class on navigation menu

// add a custom class to nav_menu li
function add_additional_class_on_li($classes, $item, $args) {

    if (isset($args->add_li_class)) {
        $classes[] = $args->add_li_class;
    }
    return $classes;
}
add_filter('nav_menu_css_class', 'add_additional_class_on_li', 1, 3);

// add a custom class to nav_menu anchor
function add_additional_class_on_anchor($classes, $item, $args) {

    if (isset($args->add_anchor_class)) {
        $classes['class'] = $args->add_anchor_class;
    }

    return $classes;
}
add_filter('nav_menu_link_attributes', 'add_additional_class_on_anchor', 1, 3);

// code on header
@php
$nav_classes = [
'theme_location' => 'primary_navigation',
// div tag
'container_class' => 'ml-auto',
// ul tag
'menu_class' => 'nav justify-content-center',
//li tag
'add_li_class' => 'nav-item',
//a tag
'add_anchor_class' => 'nav-link',
];

if (has_nav_menu('primary_navigation')) {
  //display navigation menu
echo wp_nav_menu($nav_classes);
}
@endphp
Comment

PREVIOUS NEXT
Code Example
Php :: reload page in php 
Php :: for install perticular version in vue with laravel 
Php :: wordpress on publish hook 
Php :: php regex validate username 
Php :: increment single column laravel 
Php :: yii2 redirect back 
Php :: check if date between two dates laravel eloquent 
Php :: pdo fetch 
Php :: sql where count greater than 
Php :: make a seeding file in laravel 
Php :: Check duplicate email using Jquery validation 
Php :: laravel sum group by 
Php :: sort array by key value in php 
Php :: php regex remove characters from string 
Php :: new line in php 
Php :: laravel group by created_at date only 
Php :: Laravel Add regx on password 
Php :: laravel redirect back with input 
Php :: php extract zip 
Php :: php debug telegram bot 
Php :: get count of relationship table laravel 
Php :: check if input file is set codeigniter 
Php :: php kril to eng 
Php :: joomla redirect 
Php :: AppHttpControllersValidator not found 
Php :: php get domain name from url 
Php :: laravel check old password 
Php :: delete uploaded media file wp using code 
Php :: php check credit card expiration 
Php :: twig create new array 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =