1

I am generating a menu in WordPress using wp_nav_menu() function and I want to add both text and variable into the 'menu_class' property.

Now this is how I do it:

 `'menu_class'      => 'text' . $variable,`

However no class is shown when I use this method (no error/warnings as well).

I want to know how to add the text "text" and the variable "$variable" into 'menu_class' property?

Thanks

2
  • here's the documentation for wp_nav_menu(). It says for menu_class... the class that is applied to the ul element which encloses the menu items. Multiple classes can be separated with spaces. Formerly known as $wrap_class. Commented Nov 14, 2013 at 4:10
  • so maybe just add a space? text' . ' ' . $variable Commented Nov 14, 2013 at 4:12

1 Answer 1

1

here's the documentation for wp_nav_menu(). It says for menu_class...

the class that is applied to the ul element which encloses the menu items. Multiple classes can be separated with spaces. Formerly known as $wrap_class

so try adding a space?

'menu_class'      => 'text' . ' ' . $variable,

or just

'menu_class'      => 'text ' . $variable,

this is apparenlty a good tutorial: IMPROVE YOUR WORDPRESS NAVIGATION MENU OUTPUT


This SO post deals with code that deals with two menu classes: php code optimization for wordpress nav menu. Are you sure that your variable has a value? I work with PHP all the time and that's definitely how you do string concatenation.

Sign up to request clarification or add additional context in comments.

4 Comments

Now, only the text and the space shows up, the variable disappeared.
what do you mean exactly? where does it disappear? I'm looking everywhere and it just always shows examples of separating the different class names with a space (which makes sense with CSS).
Both of the code does not work, the output is this: class="text ", there is only the text and the space, the variable is not there.
Thanks, the problem is that I put the variable below wp_nav_menu(), now I moved it up and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.