How to dynamically add latest post to nav menu
-
All I am trying to do is dynamically add the latest post to my nav menu.
That’s it.
I am wanting to do this programmatically because I don’t want to be reliant upon third parties and plugins in the operation of my site beyond that which is minimally required to have a self-hosted site on WordPress.
I am using a custom barebones theme which I have used elsewhere for about 20 years now. I modified my HTML/CSS as necessary to get it to work in the WordPress ecosystem. My site works perfectly otherwise and it looks exactly how I want it to look.
The current menu on my public site is 100% static HTML/CSS. I’ve manually input the ~300 links to the blog posts in the menu by hand all because WordPress doesn’t have a functional, native menu built in.
WHERE I’M AT:
-I’ve successfully registered my custom menu.
-I’ve got code in my functions.php file which can indeed dynamically insert the latest post title and URL into the menu.
THE PROBLEM:
-All code I can find overwrites the existing most recent menu item.
I’ve actually got a couple of very different codes which do manage to output the latest post to my “wp_nav_menu”‘s “theme_location” but they all overwrite the previous/existing entry.
I must be missing some magic line of PHP code which makes it actually append to the existing code instead of overwriting.
I can provide examples of the codes I’m using, but all I need is someone who knows PHP who can tell me “hey idiot, to append the results of the array, use the ‘”‘soilent_green’ argument”.
All I want is to dynamically add the latest entry onto the menu. It must be common knowledge to any PHP wizard.
<rant>
I have spent two full 8-hour days sweatily experimenting/googling/breaking things to no avail. What I’m attempting to do should not only be easy but it should be a baked-in, on-by-default setting. Users shouldn’t even have to think about this because it should be on by default, like every other blogging platform I’ve ever seen works. Incredibly, maddeningly, WordPress DOES have this functionality built in… for PAGES. But WordPress is fundamentally a blogging platform, is it not? Then why is this functionality not available for POSTS? I can’t believe how much time I’ve wasted trying to find an answer to this very, very basic problem.
</rant>
Here’s an example of my functions.php file with one of the several codes I’ve found which all DO dynamically add the latest post to the menu, but which all overwrite the existing menu items. I don’t want them to overwrite, I want them to be added onto the menu. Like, the basic function of any blog navigation menu anywhere. Arguably, the most basic functionality.add_theme_support('menus');
register_nav_menus(
array('sidebar-menu' => 'sidebar Menu Location',
)
);
// Front end only, to avoid issues in the admin menu editor
if ( ! is_admin() ) {
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.
*
* @param array $items The menu items.
* @param object $menu The menu object.
* @param object $args The menu arguments.
* @return array The modified menu items.
*/
function replace_placeholder_nav_menu_item_with_latest_post( $items, $menu, $args ) {
foreach ( $items as $item ) {
if ( '#latestpost' != $item->url ) {
continue;
}
$latest_post = get_posts( array(
'numberposts' => 1,
'post_status' => 'publish'
) );
if ( empty( $latest_post ) ) {
continue;
}
$appended_html = sprintf(
'<li><a href="'. esc_url( get_permalink( $latest_post[0]->ID ) ) .'">'. esc_html( get_the_title() ) .'</a></li>',
get_permalink()
);
echo $appended_html;
}
return $items;
}The page I need help with: [log in to see the link]
You must be logged in to reply to this topic.