admin管理员组文章数量:1394146
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?
2 Answers
Reset to default 2That'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' );
本文标签: post thumbnailsgetthepostthumbnail not working inside wpgetnavmenuitems hook
版权声明:本文标题:post thumbnails - get_the_post_thumbnail not working inside wp_get_nav_menu_items hook 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744672420a2618904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论