See if the following might work for you, I posted about it yesterday in response to another question so played around with it today. Also see the point in the codex of inheriting from Walker_Nav_Menu instead. You can subsequently modify the output of the menu and use your function.
Example from the codex:
<?php
class Walker_Quickstart_Menu extends Walker {
// Tell Walker where to inherit it's parent and id values
var $db_fields = array(
'parent' => 'menu_item_parent',
'id' => 'db_id'
);
/**
* At the start of each element, output a <li> and <a> tag structure.
*
* Note: Menu objects include url and title properties, so we will use those.
*/
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$output .= sprintf( "\n<li><a href="https://wordpress.stackexchange.com/questions/259311/%s"%s>%s</a></li>\n",
$item->url,
( $item->object_id === get_the_ID() ) ? ' class="current"' : '',
$item->title
);
}
}
and
<ul>
<?php
wp_nav_menu(array(
'menu' => 2, //menu id
'walker' => new Walker_Quickstart_Menu() //use our custom walker
));
?>
</ul>
Reference: https://codex.wordpress.org/Class_Reference/Walker