WordPress.org

Ready to get started?Download WordPress

Forums

Get thumbnnails in appearance->menus? wp_nav_menu (3 posts)

  1. Tom Smith
    Member
    Posted 4 years ago #

    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

  2. Tom Smith
    Member
    Posted 4 years ago #

    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()?

  3. Tom Smith
    Member
    Posted 4 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic