79199130

Date: 2024-11-18 08:08:44
Score: 0.5
Natty:
Report link

Hook into walker_nav_menu_start_el and use str_replace:

// Add to menu item when CSS Classes (in Appearance > Menus) include "new"
function ona_walker_nav_menu_start_el( $item_output, $menu_item, $depth, $args ) {
    if ( ! empty( $args->theme_location ) && in_array( 'new', $menu_item->classes ) ) {
        $title = $menu_item->title;
        $item_output = str_replace( $title, $title . '<span class="new">NEW</span>', $item_output );
    }
    return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'ona_walker_nav_menu_start_el', 10, 4 );

See https://developer.wordpress.org/reference/classes/walker_nav_menu/start_el/

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Zade