You can e.g. use the nav_menu_item_title
Codex filter to adjust the menu titles:
add_filter( 'nav_menu_item_title', function( $title, $item, $args, $depth )
{
static $instance = 0;
// Append an increasing number to the menu title
if( 'my_custom_location' === $args->theme_location
&& in_array( 'message', $args->menu_class )
)
$title .= sprintf( ' <span>%d</span>', ++$instance );
return $title;
}, 10, 4 );
where you should adjust it to your theme location and menu class.
You could also remove the menu class restriction and just use CSS to hide it when it's not within the relevant class.
Note on the inline documentation
I noticed that the inline documentation for the nav_menu_item_title
says $args
is an array but it seems to be a stdClass
object. So this is somewhat confusing!
If we check out Walker::display_element()
then we see that the $args
input argument is in fact an array, that becomes merged to the array of the output, element and depth with:
$cb_args = array_merge( array(&$output, $element, $depth), $args);
call_user_func_array(array($this, 'start_el'), $cb_args);
But here $cb_args
is an array where the fourth element is a stdClass
object (not an array) and this is the fourth input argument of the Walker_Nav_Menu::start_el()
method.
The inline documentation for Walker_Nav_Menu::start_el()
also says that it's third input argument is an array. when it's an object.
I will consider creating a trac ticket for this.