我有一些帖子类别作为主导航菜单的元素,我想显示这个类别的最后十篇帖子作为子菜单。
例如:类别1 Lastpost1 Lastpost2
主菜单是在管理面板中构建的,我想知道是否有一个钩子或什么东西可以自动实现这一点。我知道如何硬编码,但我想继续使用管理面板的酷菜单版本。
有什么想法吗?
我有一些帖子类别作为主导航菜单的元素,我想显示这个类别的最后十篇帖子作为子菜单。
例如:类别1 Lastpost1 Lastpost2
主菜单是在管理面板中构建的,我想知道是否有一个钩子或什么东西可以自动实现这一点。我知道如何硬编码,但我想继续使用管理面板的酷菜单版本。
有什么想法吗?
我最终用这个函数代码实现了它。php
// Front end only, don\'t hack on the settings page
if ( ! is_admin() ) {
// Hook in early to modify the menu
// This is before the CSS "selected" classes are calculated
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); /* Offset menu order */
$child_items = array();
// Loop through the menu items looking category menu object
foreach ( $items as $item ) {
// Test if menu item is a categroy and has no sub-category
if ( \'category\' != $item->object || (\'category\' == $item->object && get_category_children($item->object_id)) )
continue;
// Query the lasts ten category posts
$category_ten_last_posts = array(
\'numberposts\' => 10,
\'cat\' => $item->object_id,
\'orderby\' => \'date\',
\'order\' => \'DESC\'
);
foreach ( get_posts( $category_ten_last_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;
}
}
return array_merge( $items, $child_items );
}
这将显示为子菜单,一个类别的最后十个帖子作为菜单项或子菜单项放置在管理面板中。此代码的灵感来源于:http://codeseekah.com/2012/03/05/list-all-posts-in-wordpress-navigation-menu/
作为我上一次关于菜单的未解决查询的后续,这个问题已经进一步扩展。我的菜单没有打印代码中的任何地方。我正在注册菜单功能。php:add_action( \'after_setup_theme\', \'your_newtheme_setup\' ); if ( ! function_exists( \'your_newtheme_setup\' ) ) : function your_newtheme_setup() { if (