admin管理员组文章数量:1323737
I have two parent categories in my nav menu to act as separate blog pages. I would like to display the 10 most recent posts from each category underneath in the same menu. I have been searching and searching but cannot find anything. Does anyone know how to do this?
Thanks!!
I have two parent categories in my nav menu to act as separate blog pages. I would like to display the 10 most recent posts from each category underneath in the same menu. I have been searching and searching but cannot find anything. Does anyone know how to do this?
Thanks!!
Share Improve this question asked Sep 15, 2020 at 3:49 JackJack 11 Answer
Reset to default 0If I understand you correctly you want the latest posts to appear inside the menu as sub menu items, is that correct? Something like this:
Category One top menu item
| Post A sub menu item
| Post B sub menu item
I have found this solution published here: https://alex.blog/2011/09/20/code-snippet-add-a-link-to-latest-post-to-wordpress-nav-menu/, and changed it so it not only replaces the URL of a menu item but also the title:
// 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', 'replace_placeholder_nav_menu_item_with_latest_post', 10, 3 );
}
// Replaces a custom URL placeholder with the URL to the latest post
function replace_placeholder_nav_menu_item_with_latest_post( $items, $menu, $args ) {
// Loop through the menu items looking for placeholder(s)
foreach ( $items as $item ) {
// Is this the placeholder we're looking for?
if ( '#latestpost' != $item->url )
continue;
// Get the latest post
$latestpost = get_posts( array(
'numberposts' => 1,
) );
if ( empty( $latestpost ) )
continue;
// Replace the placeholder with the real URL
$item->url = get_permalink( $latestpost[0]->ID );
// And also replace the title
$item->title = $latestpost[0]->post_title;
}
// Return the modified (or maybe unmodified) menu items array
return $items;
}
One would only need to loop through all placeholders #latestpost1
, #latestpost2
, and so on, and also change $latestpost
to get the last, second last, third last of a category.
本文标签: categoriesI want to add the 10 most recent posts under a parent category on the main menu
版权声明:本文标题:categories - I want to add the 10 most recent posts under a parent category on the main menu 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742119898a2421646.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论