2016-04-24 78 views

回答

0

在function.php 的add_filter( 'wp_get_nav_menu_items', 'display_lasts_ten_posts_for_categories_menu_item',10,3);

function display_lasts_ten_posts_for_categories_menu_item($items, $menu, $args) { 

$menu_order = count($items); 
$child_items = array(); 

foreach ($items as $item) { 
if ('category' != $item->object || ('category' == $item->object && get_category_children($item->object_id))) 
continue; 
$category_ten_last_posts = array(
'numberposts' => 3, 
'cat' => $item->object_id, 
'orderby' => 'date', 
'order' => 'DESC' 
); 

foreach (get_posts($category_ten_last_posts) as $post) { 
$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); 
$child_items[]= $post; 
} 
} 
return array_merge($items, $child_items); 
} 

中的header.php wp_nav_menu(陣列( 'theme_location'=> '初級', 'menu_class'=> 'display_lasts_ten_posts_for_categories_menu_item',));

相關問題