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.
1 Answer
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. Commented Feb 8, 2013 at 4:12
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, likewp_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.