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):
<?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?
Dammit, I just discovered that the
$m->ID
I was using wasn’t actually thepost->ID
, but themenu->ID
. I used$m->object_id
to fix the issue. Hope this will help someone in the future:-S
Please use this code, you will get the exact output: