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. Conditionally get ACF field in WordPress nav walker based on parent menu item?
  5. How to add a custom field in the advanced menu properties?
  6. Can I exclude a post by meta key using pre_get_posts function?
  7. Split up wp_nav_menu with custom walker
  8. Custom post meta field effect on the performance on the post
  9. How to get custom post meta using REST API
  10. Difference between meta keys with _ and without _ [duplicate]
  11. Orderby meta_value only returns posts that have existing meta_key
  12. What is the index [0] for on post meta fields?
  13. What is “meta_input” parameter in wp_insert_post() used for?
  14. Mega Menu Walker
  15. How to enable revisions for post meta data?
  16. The “_encloseme” Meta-Key Conundrum
  17. Best way to programmatically remove a category/term from a post
  18. Adding a custom field to the site identity menu
  19. Using get_post_meta with new_to_publish
  20. Custom field metabox not showing in back-end
  21. So much data in postmeta
  22. Can I count the number of users matching a value in a multiple value key?
  23. Call custom field into menu item
  24. add span class inside wp_nav_menu link anchor tag
  25. When using add_post_meta and update_post_meta, is there any way to give the individual arrays keys?
  26. How to hide meta box values from custom fields list?
  27. Auto sort the wp-admin post list by a meta key
  28. Determine if a navigation item has children
  29. get_post_meta() unserialize issue – returns boolean(false)
  30. What is the advantage of the wp_options design pattern?
  31. Storing meta fields multiple times OR once with multi dimensional array?
  32. Allow user to create instances of custom field
  33. display specific custom fields
  34. Add Page ID class to nav menu items
  35. Is there a hook / action that is triggered when adding or removing a post thumbnail?
  36. Meta keywords and descriptions plugin for manually editing meta for each page/post
  37. passing argument to get_template_part() or a better way to code
  38. Add meta data to the menu
  39. Is it possible to store arrays in a custom field?
  40. Get updated meta data after save_post hook
  41. Multiple meta values for same meta_key adding on “Preview Changes” hit but not on saving or updating post
  42. Save HTML formatted data to post meta using add_post_meta()
  43. importing data from non-wordpress mysql db
  44. Adding Post Counts to Menu (Nav) Programmatically?
  45. Bar separated navigation by extending Walker_Nav_Menu
  46. Create meta boxes that don’t show in custom fields
  47. Transients vs CRON +Custom Fields: Caching Data Per Post
  48. Unable to save datetime custom meta field using update_post_meta() function
  49. Up/Down voting system for WordPress
  50. post meta data clearing on autosave
  51. Create custom field on post draft or publish?
  52. Display info from custom fields in all images’ HTML
  53. Create a select drop down of available menus
  54. Bar separated navigation by extending Walker_Page
  55. Ordering posts by anniversary using only day and month
  56. get_post_meta fields don’t show up on posts page
  57. Dynamic User State & City selection
  58. Extend walker – navigation, adding data attribute to a tag
  59. Custom Nav Walker sub-menu HTML construct
  60. Update meta values with AJAX
  61. How to break meta values into different items and avoid duplicates?
  62. copy attachments to another post type and change attachment url
  63. Cannot edit post meta fields with rest API
  64. ajax delete value from custom field array
  65. Save attachment custom fields on front end
  66. How to use pagination with get_post_meta
  67. Copying Custom Meta Values from existing post to a duplicate post
  68. Add a post meta key and value only if it does not exist on the post
  69. Move value of one custom field to another
  70. Order posts according to user defined order for meta values?
  71. Displaying posts with only upcoming dates according their custom field date value
  72. how to change WordPress menu markup/classes
  73. Custom fields to save multiple values
  74. Custom fields: In what order are they saved into the DB?
  75. Function to change meta value in database for each post
  76. Get a post_id where meta_value equals something in a serialized meta_value field
  77. Get aggregate list of all custom fields for entire blog
  78. Transition from (classical) serialized custom meta field to (gutenberg) rest enabled meta
  79. Unable to show ACF’s Image Custom Field properly in Genesis Framework [closed]
  80. Custom field value based on other custom field values
  81. `start_el` depth argument in custom nav walker always evaluates to zero
  82. Benefits of using Nav Walker?
  83. wp_handle_upload error “Specified file failed upload test” but still creates attachment?
  84. How to save a ToggleControl value in a meta field?
  85. Use walker for specific menu, or avoid use of walker
  86. Which is best in the following scenario : post_meta vs custom table vs parent/child posts
  87. How to display dynamic content in start_lvl function
  88. Saving custom image meta fields
  89. Author Page Custom Query WHERE author OR [post meta value] OR [post meta value]
  90. Add custom text to menu
  91. How to display Meta Field Value?
  92. has_children in custom nav_walker
  93. MySQL query to set wp_postmeta using term_taxonomy_id value
  94. How to Validate Post Meta type/extension (Video File Image File etc)
  95. Get all meta keys assigned to a post type
  96. Adding post excerpt into navigation menu
  97. Use wp_nav_menu to display ALL pages
  98. using multiple meta_key and meta_value in query_posts
  99. Custom Meta Box not Saving in Posts with Gutenberg Editor
  100. Third level navigation class
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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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