WordPress menu post featured image

I have a menu with multiple dropdowns.
I want to show bellow or beside the link of a post or posts in a menu dropdown the featured image. Is it possible?
I’ve attached an image to this message.
I don’t want to know how to style it or something like that.
So let’s say I have “Siguranta”, I want to display the featured image of that post underneath and a “Read more” link under the image. Many thanks in advance.

enter image description here

Related posts

2 comments

  1. Add filter to specific menus

    add_filter('wp_nav_menu_args', 'add_filter_to_menus');
    function add_filter_to_menus($args) {
    
        // You can test agasint things like $args['menu'], $args['menu_id'] or $args['theme_location']
        if( $args['theme_location'] == 'header_menu') {
            add_filter( 'wp_setup_nav_menu_item', 'filter_menu_items' );
        }
    }
    

    Filter menu

    function filter_menu_items($item)
    {
    
        if ($item->type == 'taxonomy') {
    
            // For category menu items
            $cat_base = get_option('category_base');
            if (empty($cat_base)) {
                $cat_base = 'category';
            }
    
            // Get the path to the category (excluding the home and category base parts of the URL)
            $cat_path = str_replace(home_url() . '/' . $cat_base, '', $item->url);
    
            // Get category and image ID
            $cat      = get_category_by_path($cat_path, true);
            $thumb_id = get_term_meta($cat->term_id, '_term_image_id', true); // I'm using the 'Simple Term Meta' plugin to store an attachment ID as the featured image
    
        } else {
            // Get post and image ID
            $post_id  = url_to_postid($item->url);
            $thumb_id = get_post_thumbnail_id($post_id);
        }
    
        if (!empty($thumb_id)) {
            // Make the title just be the featured image.
            $item->title = wp_get_attachment_image($thumb_id, 'poster');
        }
    
        return $item;
    }
    

    And then you want to remove the filter that you applied at the beginning, so that the next menu processed doesn’t use the same HTML as defined above in filter_menu_items().

    Remove filters

     add_filter('wp_nav_menu_items','remove_filter_from_menus', 10, 2);
        function remove_filter_from_menus( $nav, $args ) {
            remove_filter( 'wp_setup_nav_menu_item', 'filter_menu_items' );
            return $nav;
        }
    
  2. So, I will answer my own question. I finally did it with this code:

      // get featured image
      $thumbnail = get_the_post_thumbnail( $item->object_id );
    
      //display featured image
      $item_output .= $thumbnail;
    

    I have to mention that I used this code in the walker class.

Comments are closed.