ACF menu image returning ID not URL

56 views Asked by At

I'm trying to get an ACF image to display in a WordPress menu. I've followed the documentation on this page https://www.advancedcustomfields.com/resources/adding-fields-menu-items/ I've got a field setup in admin called menu_image this is a Image field type with the return format set to Image URL.

My code to display in the menu on the frontend is like this:

add_filter('wp_nav_menu_objects', 'kodr_wp_nav_menu_objects', 10, 2);
function kodr_wp_nav_menu_objects( $items, $args ) {
    // loop
    foreach( $items as &$item ) {
        // vars
        $menu_image = get_field('menu_image', $item);
        
        // append menu_image
        if( $menu_image ) {
            $item->title .= '<img src="'.$menu_image.'" class="menu-image" alt="menu banner">';
        }
    }
    // return
    return $items;
}

But on the frontend it's just returning the image ID rather than the full URL like this:

<img src="101690" class="menu-image" alt="menu banner">

Any ideas why?

1

There are 1 answers

0
MindBorn On

Might be ACF bug but it most definitely looks like menu_image return format is set to Image ID, not Image URL. You can always leave it as it is and slightly modify your code:

$item->title .= '<img src="'.wp_get_attachment_url($menu_image).'" class="menu-image" alt="menu banner">';