Add classes on custom navigation in Wordpress





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I've created a second custom navigation in Wordpress to create a top bar menu. But I need to add classes on the ul, li and a element.



My code in functions.php looks like this:



function secondary_navigation() {
register_nav_menu('secondary-navigation',__( 'Top bar navigation' ));
}
add_action( 'init', 'secondary_navigation' );


The code in my template looks like this:



    <?php
wp_nav_menu( array(
'theme_location' => 'secondary-navigation',
'menu_class' => 'navigation__list',
'add_li_class' => 'navigation__list__item',
)
); ?>


'menu_class' => 'navigation__list' also creates a div around my ul instead of adding the class to the ul element.



I also have a primary navigation where I've added classes on the elements above and this works perfectly. In order to make it work for this navigation I've created two functions:



/*
* Add classes to the navigation li item
*/
function add_additional_class_on_li($classes, $item, $args) {
if($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 classes to the navigation a link
*/
function add_menuclass($ulclass) {
return preg_replace('/<a /', '<a class="navigation__list__item__link"', $ulclass);
}
add_filter('wp_nav_menu','add_menuclass');


My primary navigation in my template looks like this:



    <?php wp_nav_menu(
array(
'theme_location' => 'primary',
'container_id' => 'navbarNavDropdown',
'menu_class' => 'navigation__list',
'fallback_cb' => '',
'menu_id' => 'main-menu',
'add_li_class' => 'navigation__list__item',
'depth' => 2,
)
); ?>


So how can I make these two functions (that work for the primary navigation) work for my custom secondary navigation?










share|improve this question




















  • 1





    wordpress.stackexchange.com/questions/129436/… I hope this will work for you

    – Jogi Mehul
    Jan 3 at 9:34


















0















I've created a second custom navigation in Wordpress to create a top bar menu. But I need to add classes on the ul, li and a element.



My code in functions.php looks like this:



function secondary_navigation() {
register_nav_menu('secondary-navigation',__( 'Top bar navigation' ));
}
add_action( 'init', 'secondary_navigation' );


The code in my template looks like this:



    <?php
wp_nav_menu( array(
'theme_location' => 'secondary-navigation',
'menu_class' => 'navigation__list',
'add_li_class' => 'navigation__list__item',
)
); ?>


'menu_class' => 'navigation__list' also creates a div around my ul instead of adding the class to the ul element.



I also have a primary navigation where I've added classes on the elements above and this works perfectly. In order to make it work for this navigation I've created two functions:



/*
* Add classes to the navigation li item
*/
function add_additional_class_on_li($classes, $item, $args) {
if($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 classes to the navigation a link
*/
function add_menuclass($ulclass) {
return preg_replace('/<a /', '<a class="navigation__list__item__link"', $ulclass);
}
add_filter('wp_nav_menu','add_menuclass');


My primary navigation in my template looks like this:



    <?php wp_nav_menu(
array(
'theme_location' => 'primary',
'container_id' => 'navbarNavDropdown',
'menu_class' => 'navigation__list',
'fallback_cb' => '',
'menu_id' => 'main-menu',
'add_li_class' => 'navigation__list__item',
'depth' => 2,
)
); ?>


So how can I make these two functions (that work for the primary navigation) work for my custom secondary navigation?










share|improve this question




















  • 1





    wordpress.stackexchange.com/questions/129436/… I hope this will work for you

    – Jogi Mehul
    Jan 3 at 9:34














0












0








0








I've created a second custom navigation in Wordpress to create a top bar menu. But I need to add classes on the ul, li and a element.



My code in functions.php looks like this:



function secondary_navigation() {
register_nav_menu('secondary-navigation',__( 'Top bar navigation' ));
}
add_action( 'init', 'secondary_navigation' );


The code in my template looks like this:



    <?php
wp_nav_menu( array(
'theme_location' => 'secondary-navigation',
'menu_class' => 'navigation__list',
'add_li_class' => 'navigation__list__item',
)
); ?>


'menu_class' => 'navigation__list' also creates a div around my ul instead of adding the class to the ul element.



I also have a primary navigation where I've added classes on the elements above and this works perfectly. In order to make it work for this navigation I've created two functions:



/*
* Add classes to the navigation li item
*/
function add_additional_class_on_li($classes, $item, $args) {
if($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 classes to the navigation a link
*/
function add_menuclass($ulclass) {
return preg_replace('/<a /', '<a class="navigation__list__item__link"', $ulclass);
}
add_filter('wp_nav_menu','add_menuclass');


My primary navigation in my template looks like this:



    <?php wp_nav_menu(
array(
'theme_location' => 'primary',
'container_id' => 'navbarNavDropdown',
'menu_class' => 'navigation__list',
'fallback_cb' => '',
'menu_id' => 'main-menu',
'add_li_class' => 'navigation__list__item',
'depth' => 2,
)
); ?>


So how can I make these two functions (that work for the primary navigation) work for my custom secondary navigation?










share|improve this question
















I've created a second custom navigation in Wordpress to create a top bar menu. But I need to add classes on the ul, li and a element.



My code in functions.php looks like this:



function secondary_navigation() {
register_nav_menu('secondary-navigation',__( 'Top bar navigation' ));
}
add_action( 'init', 'secondary_navigation' );


The code in my template looks like this:



    <?php
wp_nav_menu( array(
'theme_location' => 'secondary-navigation',
'menu_class' => 'navigation__list',
'add_li_class' => 'navigation__list__item',
)
); ?>


'menu_class' => 'navigation__list' also creates a div around my ul instead of adding the class to the ul element.



I also have a primary navigation where I've added classes on the elements above and this works perfectly. In order to make it work for this navigation I've created two functions:



/*
* Add classes to the navigation li item
*/
function add_additional_class_on_li($classes, $item, $args) {
if($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 classes to the navigation a link
*/
function add_menuclass($ulclass) {
return preg_replace('/<a /', '<a class="navigation__list__item__link"', $ulclass);
}
add_filter('wp_nav_menu','add_menuclass');


My primary navigation in my template looks like this:



    <?php wp_nav_menu(
array(
'theme_location' => 'primary',
'container_id' => 'navbarNavDropdown',
'menu_class' => 'navigation__list',
'fallback_cb' => '',
'menu_id' => 'main-menu',
'add_li_class' => 'navigation__list__item',
'depth' => 2,
)
); ?>


So how can I make these two functions (that work for the primary navigation) work for my custom secondary navigation?







php wordpress function






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 9:24









Sebastien Kerroue

13214




13214










asked Jan 3 at 9:19









Dennis PerremansDennis Perremans

15211




15211








  • 1





    wordpress.stackexchange.com/questions/129436/… I hope this will work for you

    – Jogi Mehul
    Jan 3 at 9:34














  • 1





    wordpress.stackexchange.com/questions/129436/… I hope this will work for you

    – Jogi Mehul
    Jan 3 at 9:34








1




1





wordpress.stackexchange.com/questions/129436/… I hope this will work for you

– Jogi Mehul
Jan 3 at 9:34





wordpress.stackexchange.com/questions/129436/… I hope this will work for you

– Jogi Mehul
Jan 3 at 9:34












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019348%2fadd-classes-on-custom-navigation-in-wordpress%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019348%2fadd-classes-on-custom-navigation-in-wordpress%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$