Automatically add images to a menu

I am calling a menu into a page, but I want to add images next to the title of the menu items.

I have this code:

ol
?php
    $navMenu = wp_get_nav_menu_items(2096); /*/Pass Nav Menu_id or Name*/
    $previousMenuParent = $level = 0;
    foreach ($navMenu as $menu) {

        if($menu-menu_item_parent == 0)
        {
            $level = 0;
            echo 'lia href='. $menu-url .'img src='. get_the_post_thumbnail($menu-ID) .'/ '. $menu-title .'/a';
            
        }
      
    }
?
/li

It works, it is showing the list of the menu items, with title and link, but for some reason, it won't show the post's image next to its title.

I also tried using get_the_post_thumbnail('thumbnail') instead of get_the_post_thumbnail($menu-ID) but that won't show an image either, with or without the img src=

I would also prefer to call a post's custom field, which has an image URL, instead of the post thumbnail. Is that possible?

Any help would be greatly appreciated.

Thank you

Topic post-thumbnails php pages posts menus Wordpress

Category Web


I guess you need this function

get_the_post_thumbnail_url($menu->object_id);

(provided menu item is not a custom link)

$menu->ID is not the id of the post/page it is linking to.

About calling meta yes it is also possible

$image_url = get_post_meta( $menu->object_id, 'meta_key', true );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.