• Hi all —

    I’m trying to figure a way to get the post/page thumbnail to show in a nav menus.

    I tried a number of things last night, and got at least a thumb variable into the menu object with this in the functions.php file of my theme:

    function my_wp_get_nav_menu_items($args) {
    
        foreach($args as $val) {
            $val->thumb = wp_get_attachment_image_src(get_post_thumbnail_id($val->ID));
        }
    
        //echo "<pre>\n";
        //print_r($args);
        //echo "</pre>\n";
    
        return $args;
    }
    
    add_filter( 'wp_get_nav_menu_items', 'my_wp_get_nav_menu_items' );

    what would be awesome is to get retults back from wp_nav_menu() like:

    <ul>
    <li><a href="something"><img src="somethingThumb.png">Something</a></li>
    <li><a href="somethingelse"><img src="somethingelseThumb.png">Somethingelse</a></li>
    </ul>

    Any ideas?
    TIA,
    Tom

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Tom Smith

    (@tom-smith)

    I found one easy way to do it, by simply adding the thumbnail path to the menu label. I like that pretty well, but I don’t think the users will go for it. 🙂

    So, instead of the menu label being:
    Something

    I made it:
    <img src="/path/to/thumb/somethingthumb.png">Something

    Anyone got a way to connect wp_nav_menu() to the_post_thumbnail()?

    Thread Starter Tom Smith

    (@tom-smith)

    Ok. So, in a fit of heroic coding/forum reading I came across this post:
    http://dustyreagan.com/global-page-navigation-accross-wordpress-mu-blogs/

    Which led me to make a copy of the wp_nav_menu() function called my_wp_nav_menu in my theme’s function.php file. Then, I found the part where the menu object is most editable, @ line 195, after sorting and before walk_nav_menu_tree(). Added this:

    foreach($sorted_menu_items as $val) {
            $title = get_the_post_thumbnail($val->object_id, 'medium') . '<br />' . $val->title;
            $val->title = $title;
            //$val->title .= ' ' . $val->ID;
        }

    This results in just what I wanted, a user-adminnable menu with automatic inclusion of thumbnails in only one of the two menus of the theme!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get thumbnnails in appearance->menus? wp_nav_menu’ is closed to new replies.