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

Dynamically get the meta key value of a menu item based on it’s post ID

This code:

wp_nav_menu( array( 'container' =>false, 'theme_location' => 'main_menu', 'menu_id' => 'main_menu', 'link_before' => get_post_meta($item->object_id, '_menu_item_img'))));

Makes me think that the problem in on the front end when you try to display the values. For readability (and without the extra )):

wp_nav_menu( 
  array( 
    'container' =>false, 
    'theme_location' => 'main_menu', 
    'menu_id' => 'main_menu', 
    'link_before' => get_post_meta($item->object_id, '_menu_item_img')
  )
);

Essentially, the problem is pure PHP. If you had debugging enabled you’d notice that there is a Notice on the line where that code runs. That is because $item is not defined at that point. $item is defined as the walker callback actually executes but PHP doesn’t notice your variable and wait until later to try to make sense of it. It is processed right there, when it is used.

You will need to create a custom walker for the front end display (something like this).

A filter on the_title might also do it:

function pre_title_wpse_134186($title) {
  return 'test'.$title;
}
add_filter('the_title','pre_title_wpse_134186');
wp_nav_menu( 
  array( 
    'container' =>false, 
    'theme_location' => 'main_menu', 
    'menu_id' => 'main_menu',
  )
);
remove_filter('the_title','pre_title_wpse_134186');

Related Posts:

  1. WordPress Menu Disappears when $query->query_vars[‘meta_key’] is set
  2. How to display custom post meta in the appearance->menus page
  3. delete all post with certain meta_keys
  4. How to make wordpress nav menu use post custom meta data
  5. update_post_meta breaking wp_update_nav_menu_item
  6. Display a portion/ branch of the menu tree using wp_nav_menu()
  7. Programmatically add a Navigation menu and menu items
  8. Any docs for wp_nav_menu’s “items_wrap” argument?
  9. Customizing Only a Specific Menu using the “wp_nav_menu_items” Hook?
  10. Changing the Order of Admin Menu Sections?
  11. How make top level menu item not have link, but have sub-menus that are linked?
  12. Add ‘has_children’ class to parent li when modifying Walker_Nav_Menu
  13. Get WP Navigation Menu from REST API V2
  14. How to Hard Code Custom menu items
  15. How to add a Custom Link to a Menu with a URL that is relative to the blog URL
  16. How do I remove UL on wp_nav_menu?
  17. Retrieving a list of menu items in an array
  18. WordPress default menu in database
  19. Adding an Arbitrary Link to the Admin Menu?
  20. Dynamically exclude menu items from wp_nav_menu
  21. Add “Logout” link to navigation menu
  22. Error: Declaration of MyClass::start_lvl() should be compatible with that of Walker_Nav_Menu::start_lvl()
  23. Split up wp_nav_menu with custom walker
  24. Removing container from wp_nav_menu not working
  25. Custom Nav walker display current menu item children, or siblings on no children
  26. wp_nav_menu: show menu only if one exists, otherwise show nothing
  27. Is there an easy way to replace a custom menu link with a page or post?
  28. Convert output of nav_menu items into a tree-like multidimensional array
  29. Get page IDs from nav items
  30. Add a custom walker to a menu created in a widget
  31. Using a menu walker add a custom item at the end of the menu’s items
  32. Add settings to menu items in the Customizer
  33. wp_get_nav_menu_items() not working with slug
  34. How to get current-menu-item title as variable?
  35. Error “Trying to get property of non-object” with Custom Walker for wp_nav_menu
  36. Add Class to Specific Link in Custom Menu
  37. Remove a menu item in menu
  38. Generate a Menu that Displays Child Pages using wp_list_pages() with the New Menu Functionality in WordPress 3.0?
  39. How do I get the name of a menu in WordPress?
  40. How to Add to Each Menu Link with link text to data-attr?
  41. How to manually specify the current active page with wp_nav_menu()
  42. How to count nav menu items?
  43. How Does The Walker Class Work?
  44. Add Javascript to WordPress Menu
  45. Add custom menu item using wp_nav_menu_items filter
  46. Filter wp_nav_menu()
  47. How to modify navigation menu of the “My Account” page in WooCommerce
  48. remove “edit your profile” from admin menu bar
  49. WordPress Settings API, Implementing Tabs On Custom Menu Page
  50. Adding line breaks to nav menu items
  51. Adding category ID or slug to WP Nav Menu
  52. add custom class to wp_nav_menu using filter hook nav_menu_css_class
  53. Custom ID for certain menu item?
  54. Does Extending Multiple Nav_Menu_Walkers Allow Nested Menus?
  55. Check if page is in a certain menu
  56. how to create a menu with all sub categories?
  57. Making breadcrumb with wp_nav_menu
  58. Does WP REST API have a built in route for calling menu?
  59. How to avoid wp_nav_menu() ID conflict?
  60. Add data-icon input to WordPress custom menu links
  61. Registering menu with ‘Automatically add new top-level pages to this menu’ selected
  62. Why do Custom Nav Menus generate so many classes on list items? Can I manage this somehow?
  63. wp_nav_menu remove class and id from li
  64. Custom search filter causes menu and query_posts problems
  65. Unregister Nav Menu from Child-Theme
  66. How does a minimal menu walker look like?
  67. Add separator to admin submenu
  68. fall back for main menu?
  69. On which hook should I be calling register_nav_menu(s)?
  70. WordPress Shortcode in Menu Item Title
  71. register_nav_menus() won’t register menus
  72. Calling the Menu Title within wp_nav_menu array function
  73. How can I create an auto-populated menu that is automatically assigned to a location?
  74. Display only page specific sub menu items using Custom Walker
  75. How do I add a search box to the nav menu?
  76. How to get IDs for objects in menu branch?
  77. New post notification in wp_nav_menu
  78. Custom Post Types posts as submenus in Nav Menu
  79. Link to Author archive from Navigation Menus in dashboard?
  80. How to stop mobile theme inheriting desktop navigation menu?
  81. Add child pages automatically to nav menu
  82. adding some custom html code to the wp_nav_menu function
  83. WordPress Menu Custom Walker Class
  84. Show just one level of child pages, wp_list_pages woe
  85. Increase search results for Admin -> Appearance -> Menus -> Search (default is 10)
  86. How to create this custom menu walker?
  87. Removing link ” from ” menu for some “links” without JS
  88. How to change order of menu items
  89. Renaming a WordPress Admin Menu
  90. Whats the difference between current_page_item and current-menu-item
  91. Using nonce in menu item
  92. Remove unusable metaboxes in nav menu management screen
  93. Menu limit, cannot add new menu items
  94. Highlight parent menu item when child is not in menu
  95. Show current navigation path from menu
  96. Deleted pages are NOT removed from custom menus?
  97. How can I add (custom) modules to Appearance -> Menus?
  98. Custom settings disappear during database migration?
  99. Call custom field into menu item
  100. How do I add nested categories to drop-down menu in twenty-eleven?
Categories menus Tags menus, post-meta
Why would apply_filters return a non-empty string, when the value returned is empty?
wordpress plugin license issue -New plugin development

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