最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

post thumbnails - get_the_post_thumbnail not working inside wp_get_nav_menu_items hook

programmeradmin1浏览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?

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?

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Aug 3, 2014 at 10:14 Mihai PapucMihai Papuc 2582 silver badges8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

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.

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' );
发布评论

评论列表(0)

  1. 暂无评论