检查每个菜单项的ID值怎么样,如下所示:
if( $item->ID === 45): // ADD THIS MENU-ITEM ID CHECK
// Query the lasts ten category posts
$category_ten_last_posts = array(
\'posts_per_page\' => 11,
\'category_name\' => \'mamba\',
\'orderby\' => \'date\',
\'order\' => \'DESC\'
);
$posts = get_posts( $category_ten_last_posts );
foreach ( $posts as $post ) {
//...
}
endif;
ps:我移动了
get_posts()
超出foreach输入参数。
更新此操作适用于我的安装:
!is_admin() AND add_filter( \'wp_get_nav_menu_items\', \'display_lasts_ten_posts_for_categories_menu_item\', 10, 3 );
// Add the ten last posts of af categroy menu item in a sub menu
function display_lasts_ten_posts_for_categories_menu_item( $items, $menu, $args ){
$menu_order = count($items);
$child_items = array();
foreach ( $items as $item ):
if( $item->ID === 45 ):
// Query the lasts ten category posts
$category_ten_last_posts = array(
\'posts_per_page\' => 11,
\'category_name\' => \'mamba\',
\'orderby\' => \'date\',
\'order\' => \'DESC\'
);
$posts = get_posts( $category_ten_last_posts );
foreach( $posts as $post ):
// Add sub menu item
$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 = $post->post_title;
$post->url = get_permalink( $post->ID );
/* add children */
$child_items[] = $post;
endforeach;
endif;
endforeach;
return array_merge( $items, $child_items );
}