How might I have custom markup for my menus, like I want to give menu items something like id="menuitem-postname"
how might I do that?
Leave a Reply
You must be logged in to post a comment.
How might I have custom markup for my menus, like I want to give menu items something like id="menuitem-postname"
how might I do that?
You must be logged in to post a comment.
The only way that I could think to do this would be to add a filter to
wp_nav_menu_items
. Here is the code that I got to work (just add it to your themes functions.php file):It uses the last part of the url to create the page name for the id. So menu-item-2 for http://yoursite.com/page1/page2/ would become menu-item-page2.
The
if( $args->theme_location == 'primary' )
is for targeting a specific menu. If you want to do this for all menus just remove the if else statement.Very Simple … Use a Custom Walker … All you need to do is attach something constant to the id element … such as the actual post or page id … That’s in the menu_item->object_id … I don’t really see why your doing this though … with a cursory knowledge of CSS you can easily utilize the cascade to address an items children … All the menu_items are already assigned css classes … @see the Codex
You can just add a custom function to the nav_menu_item_id hook that does something like this:
If there are more menu item types than
page
andcategory
, it’s easy to addcase
blocks for each of them.