Due to the way my projects menu is coded/styled I'm having some difficulty figuring this out. I have appended a div to my menu using jQuery, and I want to be able to include an external php file that simply contains a wp_list_categories(); function wrapped in an unordered list. What would be the best way to go about doing this so that wordpress recognises the wp_list_categories functions and works as I've intended? or is there a better/easier solution.

  • perhaps you should back up a bit and explain why you need to append a div via javascript to create your menu. I have a feeling there's a better solution to that problem which will eliminate this problem. – Milo Feb 8 '13 at 3:04
  • @Milo I'm trying to achieve a drop-down effect for my menu, where if a user hovers over a menu item its children in the menu display - for one specific menu item there has to be an entire full-width div that spans across the entire menu (where I wish to display different categories). Apologies if it doesn't make sense, I've included the following picture that might help get across what I'm trying to do. i.imgur.com/e8f34zD.jpg – Steve Stevenson Feb 8 '13 at 3:17
  • assuming this is a wp_nav_menu, I suggest searching around a bit on this site and elsewhere for custom menu walkers and the various filters available for nav menus, like wp_nav_menu_objects. you can inject the div and categories all at once when the menu is output, no javascript necessary. Give it a shot and update your question with code if you need further help. – Milo Feb 8 '13 at 3:26

Use wp_localize_script to pass PHP data to Javascript. Store the result in a variable by setting the echo parameter of wp_list_categories() to false, and then pass the variable in the object you send to javascript through wp_localize_script.

On a side note, I am not sure why you are having this problem at all, honestly: can you not hide your dropdowns with CSS? Or even detach them with jQuery on jQuery(document).ready(), to reapply them when you need them?

  • You're absolutely right, I'm not sure why I didn't think of this earlier. Thank you for explaining the wp_localize_script method to me though. – Steve Stevenson Feb 8 '13 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.