您可以使用过滤器挂钩wp_get_nav_menu_items
在WP Nav菜单中添加新项目。下面的示例是在nav菜单中添加最后的帖子。
您可以将自定义函数添加到此过滤器中,例如使用特定ID添加来自作者的每个帖子。添加帖子的逻辑位于自定义函数中,如中的以下示例replace_placeholder_nav_menu_item_with_latest_post
.
// 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 );
}
// Return the modified (or maybe unmodified) menu items array
return $items;
}
源示例来自Viper007Bond,请参见
the post 有关代码的详细信息。