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 );
}
我正在使用此功能添加子菜单(修改自
Category menu item and its last 10 posts as sub-menu ). 出于我无法理解的原因,我无法获取这些帖子的缩略图-尽管超出了此功能
get_the_post_thumbnail ( 973, \'thumbnail\' );
返回预期结果。有什么想法吗?
最合适的回答,由SO网友:adrian7 整理而成
这是因为对象属性区分大小写,因此$post->ID
不同于$post->id
.
只需打印出这两个,您就会注意到差异,或者打印$post
对象以查看所有可用的属性和方法。