V
V
Vitya Podpriklopolny2018-03-04 10:39:39
CMS
Vitya Podpriklopolny, 2018-03-04 10:39:39

How to add classes to menu in Wordpress?

There is a Menu on WordPress that
has a .menu class, I
hack the menu through wp in this way

<?php wp_nav_menu( array(
          'theme_location'  => '',
          'menu'            => '',
          'container'       => 'ul',
          'container_class' => 'menu',
          'container_id'    => '',
          'menu_class'      => 'menu',
          'menu_id'         => '',
          'echo'            => true,
          'fallback_cb'     => 'wp_page_menu',
          'before'          => '',
          'after'           => '',
          'link_before'     => '',
          'link_after'      => '',
          'items_wrap'      => '<ul id = "%1$s" class = "%2$s">%3$s</ul>',
          'depth'           => 0,
          'walker'          => '',
        ) ); ?>

, but since the elements inside the menu also have classes, they are not styled
How to add classes to the elements inside the menu?
I don’t understand at all why the WP developers didn’t add this item to the array

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeman0204, 2018-03-18
@megamage

, but since the elements inside the menu also have classes, they are not styled
How to add classes to the elements inside the menu?

What's the problem with styling them with a selector? Like .menu li {}, if you need to add another class to ul, you can add here 'menu_class' => 'new_class menu',
you want to add a class to jquery for li $('.menu li').addClass('child');example. Each menu item already has its own classes and id, and the selection can be done as you like. What is the problem?
And why display all the parameters, you can and so
<?php wp_nav_menu( array(
          'theme_location'  => '',
          'menu_class'      => 'menu',
        ) ); ?>

to indicate the location and add. class if needed.

O
Orkhan Hasanli, 2018-03-04
@azerphoenix

Hello.
To do this, you need to create a Walker.
If you have a bootstrap theme, then the finished walker is here - https://github.com/wp-bootstrap/wp-bootstrap-navwalker
If you want to get a clean walker and then style it, then https://gist.github.com /thefuxia/1053467
Create a PHP file, add the code above, make an include via functions.php
Style it to your liking. And do not forget to specify walker in the displayed menu (pay attention to the last line of your code where you need to specify walker)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question