0

I like this solution from Ahmad M, a lot, but how do I add images in addition to titles, instead of replacing titles? (Sorry, I'm still pretty new to PHP and Stackexchange).

Pulling Featured Images in to a WordPress Menu

I mostly figured it out. I'm using Ahmad M's filter but needed to add ".$menu_object->title" to the thumbnail so now it looks like:

$menu_object->title = has_post_thumbnail($menu_object->object_id) ? get_the_post_thumbnail($menu_object->object_id, 'full') .  $menu_object->title .  : $menu_object->title;

Now my challenge is that I only want to add it to third level menu items (child of a child of a parent).

5
  • I understand that you want to show featured image with each of your menu link. Is it correct?
    – BlueSuiter
    Commented Apr 10, 2017 at 13:28
  • Yes. Ahma M's solution works really well, but I needed to modify it. I figured out how to do that, but now I need to make it work only third level menu items.
    – red5
    Commented Apr 10, 2017 at 14:58
  • Only for third level menus?
    – BlueSuiter
    Commented Apr 11, 2017 at 4:39
  • Yes. I would like to understand how to set a parameter for any given level, but for my current project, I need third level menu items.
    – red5
    Commented Apr 21, 2017 at 16:10
  • can you please share the html you are going to use for same..
    – BlueSuiter
    Commented Apr 21, 2017 at 18:12

1 Answer 1

0

The following code will get you the featured image for 3rd level of your navbar. You, have to pass the nav-menu id within wp_get_nav_menu_items(). It will get you the menu list (For more details).

<ul>
    <?php
        $navMenu = wp_get_nav_menu_items(9); /*/Pass Nav Menu_id or Name*/
        $previousMenuParent = $level = 0;
        foreach ($navMenu as $menu) {

            if($menu->menu_item_parent == 0)
            {
                $level = 0;
                echo '<li><a href="'. $menu->url .'">'. $menu->title .'</a>';
            }
            elseif($menu->menu_item_parent != '' && $menu->menu_item_parent != $previousMenuParent)
            {
                $level++;
                echo '<ul class="submenu">';
                echo '<li><a href="'. $menu->url .'">'. $menu->title .'</a>';
                $previousMenuParent = $menu->menu_item_parent;
            }
            elseif($previousMenuParent == $menu->menu_item_parent)
            {
                echo '</li><li><a href="'. $menu->url .'">';
                if($level == 3)
                {
                    echo get_the_post_thumbnail($menu->ID);
                }
                echo $menu->title .'</a>';
            }
        }
    ?>
    </li>
</ul>
1
  • Thank you! I'm going to give it a try. I like how Ahmad M used a filter in the original post referenced, above. I'll see if I can adapt your work here into a filter. Original question: wordpress.stackexchange.com/questions/87017/…
    – red5
    Commented Apr 24, 2017 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.