Question

if ( ! is_admin() ) {
add_filter( 'wp_get_nav_menu_items', 'display_last_posts_for_menu_item_ts', 10, 3 );
}

function display_last_posts_for_menu_item_ts( $items, $menu, $args ) { 
    $menu_order = count($items); /* Offset menu order */
    $post_ids = array(250,973);
    $args = array ( 'include' => $post_ids );
    $child_items = array();
    foreach ( $items as $item ) {        
        foreach ( get_posts( $args ) as $post ) {
            // Add sub menu item
            $img = get_the_post_thumbnail ( $post->id, 'thumbnail' );
            $post_thumbnail_id = get_post_thumbnail_id( $post->id );
            $img .= '<!--' . $post_thumbnail_id . ' / ' . $post->ID . '-->';
            $post->menu_item_parent = $item->ID;
            $post->post_type = 'nav_menu_item';
            $post->object = 'custom';
            $post->type = 'custom';
            $post->menu_order = ++$menu_order;
            $post->title = $img . $post->post_title;
            $post->url = get_permalink( $post->ID );
            /* add children */
            $child_items[]= $post;
        } 
    }
    return array_merge( $items, $child_items );
}

I am using this function to add a submenu ( modified from Category menu item and its last 10 posts as sub-menu ). For a reason I cannot understand, I can't get the thumbnails of those posts - although outside this function get_the_post_thumbnail ( 973, 'thumbnail' ); returns the expected result. Any ideas on this?

Was it helpful?

Solution

That's because object properties are case sensitive, thus $post->ID is not the same as $post->id .

Just by printing out these two you'll notice the difference, or do a print_r of the $post object to see all of the available properties and methods.

OTHER TIPS

just remove ->id becouse you are yousing only the first ID in function

$img = get_the_post_thumbnail ( $post->id, 'thumbnail' ); 

it must be like that :

$img = get_the_post_thumbnail ( $post, 'thumbnail' );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top