Code Snippet: Add A Link To Latest Post To WordPress Nav Menu

Someone on #wordpress IRC support channel was trying to add link to their latest blog post to their WordPress navigation menu, so I threw together a few lines of code to help them accomplish this.

// 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 )

		// Get the latest post
		$latestpost = get_posts( array(
			'numberposts' => 1,
		) );

		if ( empty( $latestpost ) )

		// 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;

Place the above code in a plugin or just in your theme’s functions.php file.

Create a new “Custom Link” menu item where the URL is #latestpost (which will act as a placeholder and target for the code). You can title the item whatever you please. This code will then find that menu item (by looking for the placeholder URL) and then replace it’s URL with that of the latest post’s URL. Since the filter is before the code that adds the selected CSS classes runs, the menu highlighting will even work. When you visit the latest post on your blog, this menu item will light up.

Rather simple and elegant. You gotta love WordPress hooks.

74 thoughts on “Code Snippet: Add A Link To Latest Post To WordPress Nav Menu

  1. Any thoughts on the performance implications of doing this with 3 x 4 placeholders pulling posts from 3 categories? I’m thinking adding those 3 extra get_posts() requests is suboptimal. I’m considering caching the data somehow, but that introduces its own complications.

    Anyway, thanks for the inspiration, it’s a great starting point for what I want to do.

  2. Mate – I just want to say thanks so much. Your work is really simple and clever and I love that you’ve given such detailed responses to people’s questions. It really helped me work out how to implement this.
    So, in summary – you’re a legend! Thanks!

  3. This is awesome, I’ve made it a plugin and am using it for links to the first and last post. Is there any possible way to use this technique to put links in the menu for the next post and the previous post? And maybe even a random link?

  4. Pingback: How to add a "Latest Post" link to my Wordpress nav menu

  5. I had to replace
    $latestpost = get_posts( array(
    ‘numberposts’ => 1,
    ) );

    $latestpost = get_posts( array(
    ‘posts_per_page’ => 1,
    ) );

    Then, it worked perfectly, thx 😉

  6. This works perfectly for me in the menu! Is there a way to make this a page or site-wide change? For instance, if I had a button somewhere else that linked to ‘#latestpost’ that wasn’t in my menu, how would I make this function search outside of the menu for ‘#latestpost’ and apply the change?

  7. Pingback: WordPress plugin: insert link to latest post (in category) on your menu |

  8. Pingback: Link Roundup: March 2016 - WPVegas

Leave a Reply