Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Nav Menu – Add class based on meta keys

Your code doesn’t because the global $post refers to the post/page being viewed (if there is one…). What is passed with nav_menu_css_class is a menu item – this is a particular post type. Among its properties are:
object which stores:

  • the post type if the link refers to a post
  • the taxonomy if the link refers to a taxonomy-term page
  • ‘custom’ if its a custom link

It also stores the object_id

  • the post id if the link refers to a post
  • the term id if the link refers to a taxonomy-term page
  • the post id of the custom link

So in what you want to do is check if the term actually refers to a post (or a custom post type), and then pass the object_id to the get_post_meta():

function wpse51076_special_nav_class($classes, $item){
    if( 'post' == $item->object ){
            $colorcode = get_post_meta($item->object_id, 'color_dropdown', true);
           $classes[] = $colorcode;
    }
    return $classes;
}
add_filter('nav_menu_css_class' , 'wpse51076_special_nav_class' , 10 , 2);

Related Posts:

  1. Walker class for sub-menu with ACF fields
  2. Show Custom field value instead of title in WP Menu
  3. Override edit_custom_walker.php to conditionally show input depending on menu position
  4. How to add a custom field in the advanced menu properties?
  5. Can I exclude a post by meta key using pre_get_posts function?
  6. What is the index [0] for on post meta fields?
  7. Custom field metabox not showing in back-end
  8. So much data in postmeta
  9. add span class inside wp_nav_menu link anchor tag
  10. How to hide meta box values from custom fields list?
  11. get_post_meta() unserialize issue – returns boolean(false)
  12. What is the advantage of the wp_options design pattern?
  13. Meta keywords and descriptions plugin for manually editing meta for each page/post
  14. Add meta data to the menu
  15. Adding Post Counts to Menu (Nav) Programmatically?
  16. Bar separated navigation by extending Walker_Nav_Menu
  17. Up/Down voting system for WordPress
  18. post meta data clearing on autosave
  19. Display info from custom fields in all images’ HTML
  20. Create a select drop down of available menus
  21. Dynamic User State & City selection
  22. Update meta values with AJAX
  23. copy attachments to another post type and change attachment url
  24. Cannot edit post meta fields with rest API
  25. Add a post meta key and value only if it does not exist on the post
  26. Custom fields to save multiple values
  27. wp_handle_upload error “Specified file failed upload test” but still creates attachment?
  28. Which is best in the following scenario : post_meta vs custom table vs parent/child posts
  29. Saving custom image meta fields
  30. Get all meta keys assigned to a post type
  31. Adding custom fields (post meta) before/during wp_insert_post()
  32. MySQL Query that looks for post with Custom Field, then changes Category
  33. ACF: How to get the full field name (meta_key) by a field key?
  34. post meta getting deleted on save
  35. filtering custom post types via meta data drop down
  36. Add within the output of
  37. How to add a new meta key and assign timestamp to posts
  38. Custom field not updating when value is empty
  39. meta_compare seems to be treating values as strings instead of integers as expected
  40. Limit the number of acf content when displaying in post loop [closed]
  41. Read / Watch / Listen times – meta
  42. How to add custom metadata text box dropdown to sidebar in Gutenberg editor for all post types
  43. Is this format possible with a custom Nav Walker class?
  44. Why is my Custom Meta Box Field Inputs NOT saving?
  45. Get registered custom fields or post meta even if empty
  46. Displaying multiple URLs as custom field values
  47. Show values of custom post meta on ‘Add new post’ page?
  48. Custom post meta field effect on the performance on the post
  49. How do you add a around the second level of a Menu?
  50. Detecting classes, adding widgets, and adding divs in with a Nav Walker
  51. How can you include custom post meta in search without calling each key?
  52. How to remove dropdown from wp_nav_menu
  53. Can I access a post meta field before the loop?
  54. wrap text around custom fields array
  55. How can I modify RSS item titles to be either the title or a custom meta field?
  56. Avoiding ACF get_field and returning to core WordPress function
  57. Get all the posts where meta field with multiple choice has several values checked
  58. Show children (sub-navigation) of active menu item only
  59. Move Title and the Content WYSIWYG editor position
  60. How do i get the attached images with custom value checked?
  61. How to make wordpress nav menu use post custom meta data
  62. Save all the post tags inside a custom field
  63. Adding a unique ID to each sub-menu
  64. Update post meta custom field using block editor
  65. Get custom fields without _edit_last, _edit_lock, _wp_page_template and _visual-subtitle
  66. Display posts with empty custom field
  67. Why am I getting a “Call to member function format() on a non-object” error?
  68. Modify custom field from front end
  69. Footnotes in custom fields
  70. Create a new custom field for all posts based on current custom field
  71. wordpress get_post_meta / the_meta to output custom field value
  72. Custom Query: query by post custom meta data
  73. update_post_meta not working well
  74. Set class if a meta value is set within post archive
  75. Is it possible to get_the_post_thumbnail (Featured Image) inside a sprintf function?
  76. How to add a post’s view count into the WordPress API response
  77. What is inside in $item in start_el function in Walker_Nav_Menu class
  78. Can’t set custom meta fields for a post
  79. Allow HTML in Custom Metabox area
  80. Custom field metabox not showing in back-end
  81. Conditional Nav Menu Inside Nav Menu Across Multisite Sites
  82. Query Multiple meta
  83. Creating custom Menu for custom theme
  84. Query postmeta values, and return multiple post_titles for common meta value
  85. Add custom class to existing menu items from custom meta
  86. When post is updated, custom metadata in text area field is overwritten
  87. Add dynamic content block to nav section generated by custom walker
  88. Media Attachment Custom Meta Fields not saving in Media Uploader when using jQuery UI Autocomplete
  89. How to use page metadata while using wp_list_pages()?
  90. Custom Field: Display only if a specific key is selected outside the loop
  91. Walker for menus
  92. When editing a post with a custom meta box the values aren’t displaying correctly
  93. add_post_meta insert null value into Database
  94. Retrieving custom field as shortcode
  95. how to make a walker to this (in bootstrap, i try but not work)
  96. how can i get the page_ID in nav li [closed]
  97. Add custom fields to specific menus
  98. save all acf options in one meta_value [closed]
  99. get Custom field label (select/dropdown) on front end
  100. Block Editor – Meta values not saved, meta changes to empty array on update
Categories custom-field Tags custom-field, menus, navigation, post-meta, walker
Including Custom Meta with posts_where query
Removing Post Meta from Category Pages?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress