0

So with the new TT3 theme, doesn't look like we can use nav_menu_css_class filter anymore to add custom classes to menu items (<li>) or nav_menu_link_attributes for the anchors (<a>) like we did before:

add_filter('nav_menu_css_class', function ($classes){
  $classes[] = 'custom-nav-item';
  return $classes;
}, 10 , 2);

add_filter( 'nav_menu_link_attributes', function($atts) {
  $atts['class'] = 'custom-nav-link';
  return $atts;
}, 100, 1 );

likely because menus are now created via the new Customize->Editor and don't fire the hook which was a formerly initiated by a hardcoded wp_nav_menu in theme template files.

Has anyone figured out how to add custom classes to menu items in twentytwentythree?

Vadim H
  • 717
  • 5
  • 12
  • Does this answer your question? [Wordpress Menu - Add class to anchors](https://stackoverflow.com/questions/42994156/wordpress-menu-add-class-to-anchors) – Kirk Beard Mar 06 '23 at 23:17
  • Just to double check, your code uses `‘` instead of `'` which will cause PHP errors. Can you check that the code in your question matches that in your website? – Kirk Beard Mar 06 '23 at 23:18
  • @KirkBeard No, the filter `nav_menu_link_attributes` also doesn't run when the menu is created with the new _Customize->Editor_ – Vadim H Mar 08 '23 at 05:04
  • 1
    @KirkBeard correct ticks were used in code, just copying/pasting somehow changed them to the other type. And `
  • ` targeting is now specified since I don't need to touch `` of the menu items. Thanks!
  • – Vadim H Mar 08 '23 at 05:07
  • Yes, you are correct that the nav_menu_css_class and nav_menu_link_attributes filters are not used in the new Customize->Editor method of creating menus in the Twenty Twenty Three theme for WordPress. –  Mar 08 '23 at 15:51
  • However, you can still add custom classes to menu items using the nav_menu_item_classes filter. Here's an example of how to do it: `add_filter( 'nav_menu_item_classes', function( $classes, $item, $args, $depth ) { $classes[] = 'custom-nav-item'; return $classes; }, 10, 4 );` –  Mar 08 '23 at 15:51
  • This code adds the 'custom-nav-item' class to all menu items. Similarly, you can add custom attributes to the menu item's anchor using the nav_menu_link_attributes filter, like so: `add_filter( 'nav_menu_link_attributes', function( $atts, $item, $args, $depth ) { $atts['class'] = 'custom-nav-link'; return $atts; }, 10, 4 );`This code adds the 'custom-nav-link' class to all menu item links. –  Mar 08 '23 at 15:51
  • @user21354674 thanks, but filters `nav_menu_item_classes` and `nav_menu_link_attributes` also don't run in this case (can't find documentation for `nav_menu_item_classes` online either) – Vadim H Mar 12 '23 at 04:55