0
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?

0

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.

  • Ouch, that is really something. It really is not a novelty for me that the object properties are case sensitive. I spent about 4 hours troubleshooting the whole thing and never saw the lower caps :( Thanks for opening my eyes! – Mihai Papuc Aug 3 '14 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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