In addition to this function in order to achive this functionality you need to used the Advanced Custom Fields plugin (or the Secure Custom Fields alternative). You have to create a field group and then create an image field (it's important to set the Return Format option to "Image URL") and set the "Location rules" to "Menu item" => is equal to => the name of the menu where you want the field to be visible. Now you can add images to your menu items and to display them add this function to you functions.php file.
![[add-image-to-menu.png]]
```php
add_filter('wp_nav_menu_objects', 'my_wp_nav_menu_objects', 10, 2);
function my_wp_nav_menu_objects( $items, $args ) {
if( $args->menu == "main-menu" ) { //replace with your menu name
foreach( $items as $item ) {
$menu_item_logo = get_field( 'menu_item_logo', $item ); //replace with your field name
if( $menu_item_logo ) {
$item->title = '<img src="' . $menu_item_logo . '" class="menu-item-logo"></img> <span>' . $item->title . '</span>';
}
}
}
return $items;
}
```