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

Add custom class to existing menu items from custom meta

I’ve got the filter just for you: nav_menu_css_class

function wpse_175057_nav_menu_css_class( $classes, $item ) {
    if ( $item->type === 'post_type' && $class = get_post_meta( $item->object_id, '_icon', true ) )
        $classes[] = $class;
    return $classes;
}

add_filter( 'nav_menu_css_class', 'wpse_175057_nav_menu_css_class', 10, 2 );

Related Posts:

  1. Apply the_title() filter in post & page title, but not in menu title
  2. Adding Post Counts to Menu (Nav) Programmatically?
  3. Is it possible to show a different WP menu when using password protected pages?
  4. Edit menu item title from edit page/post
  5. Disable most recent & view all (TABS) on nav-menu.php
  6. Using wp_list_pages() after calling query_posts()
  7. disable Tab post on nav-menus page (Admin)
  8. Making a new post visible on a specific page
  9. Featured Images most often doesnt appear
  10. Go back to the exact post page number
  11. Parent menu item does not display posts when I add the sub-items
  12. Using WP page password protection as defacto login
  13. I lost the link to my posts in my menu bar
  14. Page and Posts loading as index page? Not loading the content
  15. Call posts of particular category in a page
  16. How to make posts appear under pages
  17. Adding a post to a page
  18. When changing pages to posts, how do you set up 301 redirects for the page URLs?
  19. Display Posts on Custom Page
  20. Show posts count for Categories and Tags in wp_nav_menu
  21. How to add category post count in main navigation menu [closed]
  22. User Selectable Delete of Pages
  23. 2 dynamic sidebars registered, not showing up
  24. Multiple Loops Breaking Pages
  25. “about us”, ” contact” sections should be article(post) or page in the simple small Business website?
  26. How to get ID of the page included with get_page()?
  27. Posts vs Pages and categories
  28. Including link to custom post type in ‘wp_list_pages’ function
  29. How to dynamically parse and transform post/page content before displaying it?
  30. How to make default “Privacy Policy Page” a normal page?
  31. Customize rel=canonical tag for single blog post
  32. Change all author links in Blog roll
  33. How to I retrieve the ID from the Posts page?
  34. How to save meta checkbox WordPress
  35. Proper way of establishing custom landing pages in WordPress
  36. Parsing Menu Items and Blog Posts
  37. How to Use index.php file for “Blog” Page And this is Not a Home Page
  38. How to calculate the average of a post meta value(Numeric) of a specific author
  39. is_page Funtion for Posts ?
  40. Is there any way to tell when wp_postmeta has been updated?
  41. is_page() and is_page_template() not being differentiated
  42. Admin – no Featured image choice in create new/page|post
  43. How to detect when admin user is on the All Posts page?
  44. static landing page leading to author specific pages w/ “live” content
  45. wpColorPicker – problem with implementation to post meta
  46. Total number of posts in query (category/tag/author/search results/main page…)
  47. Custom Permalink Structure for Pages & Posts
  48. Getting a “404 Not Found” error when “Preview Changes” is clicked
  49. Why won’t pagination work?
  50. How can I list posts by author?
  51. WordPress Query Posts From Category Post on Static Page
  52. How to create a “latest news” page showing a list of posts from blog category
  53. WordPress Menu options in the Admin: Posts for the dropdowns
  54. Is there a way to add the list of recent posts into the admin sub menu on hover?
  55. Automatically add images to a menu
  56. How do you search for a post or page with a certain url on the WordPress admin panel?
  57. Change post author without using wp_update_post()
  58. How to add some html formated text to every new post in WordPress
  59. if in category but only with post meta
  60. Insert menu with a custom walker into page / post body using shortcode?
  61. Intercept page request and add value to it
  62. wp_query with ajax
  63. Update post meta within save_post action
  64. WordPress 302 Redirect to Random Product/Post/Page
  65. Display post number by category
  66. Output Post with ACF Fields into other Post
  67. How to create a page that shows the last 10 posts?
  68. Updating post meta for checkbox
  69. Deleted a user with administrator rights. Now all the data/content/images are lost. how to recover them back
  70. $_GET parametters not working in an specific template
  71. Saving Post Data in Another Database
  72. New Posts and Pages Won’t Save
  73. My Custom Post Type Still Using index.php
  74. get_children returns older images
  75. Posts and Pages: 404 Page not found
  76. WordPress post filter menu
  77. Custom permalink for each post
  78. the_content() Not Grabbing All Content
  79. How can I tell if a post has been published at least once?
  80. Un-highlight Blog Menu Item when Category Menu Item is Selected
  81. Is there a way getting post fields using `transition_post_status`
  82. Display Featured Posts Grid on Static Page (Gazette)
  83. Automatic blog page with custom design [closed]
  84. Detecting current page from class method
  85. Add multiple meta keys to a post at once
  86. Having pages after specifying post_type post
  87. Show number of posts by logged in user
  88. How does WP decide how to display a page
  89. Unabled to Change Permalinks – Even Using the “Edit” (Resets to Original Permalink on “Update”)
  90. List all posts, pages and custom post types in admin
  91. Make ‘blog’ page show content as well as posts underneath it
  92. I don’t want the complete article to show on homepage
  93. Get current post’s nav menu name (term name)
  94. Post/page title to permalink transformation – what is behind the scenes?
  95. When sending a newsletter -not with wordpress- the server has 100% cpu [closed]
  96. Change Author Name to Sitename on Frontend
  97. Would a “hub” page work better as a page or a post?
  98. Display on page every post in blog WITH comments
  99. Are post, page and category IDs unique to each other?
  100. Retrieve data from post meta meta_key having ‘_product_fq_image’ while having author_id and post type is PRODUCT
Categories posts Tags menus, pages, post-meta, posts, walker
Adding category in wordpress – custom
WordPress audio player add class randomly using Audio Album plugin

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