get_post_thumbnail_id() Doesn’t return value

I’m changing a menu from a loop with query_posts to a “custom menu” using wp_get_nav_menu_items, but I can’t seem to retrieve the post_thumbnail.

The original code I had (snippet):

Read More
<?php
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post();
    $image = wp_get_attachment_image_src(get_post_thumbnail_id());
endwhile; endif;?>

My new code:

<?php
$menu = wp_get_nav_menu_items('Homepage blokken');
foreach($menu AS $m){
     echo $m->ID; // this echoes the correct ID
     $pThumbId = get_post_thumbnail_id($m->ID); // this returns empty
} ?>

Why is this not working?

Related posts

Leave a Reply

2 comments

  1. Dammit, I just discovered that the $m->ID I was using wasn’t actually the post->ID, but the menu->ID. I used $m->object_id to fix the issue. Hope this will help someone in the future :-S

  2. Please use this code, you will get the exact output:

    foreach ( $items as $el ) {
        if ( $el->post_parent == $parent ) {
            array_push( 
                $ret,
                array(
                    'id'   => $el->object_id,
                    'text' => $el->title,
                    'url'  => $el->url
                )
            );
        }
    }