You are almost there, see comments in code below…
function wp3456789_wp_nav_menu_objects( $items, $args ) {
foreach( $items as &$item ) {
$image = get_field('icon', $item);
if( $image ) {
// Your original code which appends html string to current $item->title
//$item->title .= '<span class="inline-img' . $item->class . '"><img class="style-svg" src="' . $image['url'] . '" width="" height="" alt="' . $image['alt'] . '" /></span>';
// To prepend your html to title simply override $item->title with new value
// and include $item->title at the end of your html string value...
$item->title="<span class="inline-img " . $item->class . '"><img class="style-svg" src="' . $image['url'] . '" width="" height="" alt="' . $image['alt'] . '" /></span> ' . $item->title;
}
}
return $items;
}
add_filter('wp_nav_menu_objects', 'wp3456789_wp_nav_menu_objects', 10, 2);
Though you should also note there is no $item->class
property.
There is a $item->classes
property which is an array. You will need to implode()
array into a string, see example and comments in code below…
function wp3456789_wp_nav_menu_objects( $items, $args ) {
foreach( $items as &$item ) {
$image = get_field('icon', $item);
if( $image ) {
// if $item->classes is array then implode array values into string
$classes = is_array($item->classes) ? implode(" ", $item->classes) : '';
// prepend span and image to $item->title
$item->title="<span class="inline-img " . $classes . '"><img class="style-svg" src="' . $image['url'] . '" width="" height="" alt="' . $image['alt'] . '" /></span> ' . $item->title;
}
}
return $items;
}
add_filter('wp_nav_menu_objects', 'wp3456789_wp_nav_menu_objects', 10, 2);