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 )
			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;
}

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.

71 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?

    • Sure. Just use different placeholders and then the next/previous link functions:

      https://codex.wordpress.org/Function_Reference/get_next_posts_link
      https://codex.wordpress.org/Function_Reference/get_previous_posts_link

      Don’t forget to remove the menu item when is_single() returns false though, i.e. those functions won’t work when viewing your homepage.

      Random would just be 'orderby' => 'rand' on the get_posts() call but be warned, this will murder your database trying to do this as it’s a super slow and resource intensive query as it has to fetch all posts and then sort them.

      • Unfortunately my PHP understanding is just good enough to get myself into trouble. I tried this change before I asked you and I got a link to nothing.

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

        I am unsure how to go about removing the menu item when is_single returns false. Any help would be awesome. Also, I just moved to Eugene from Portland, small world…Go Sounders!

          • So like this? This results in bringing me to the first post…Sorry if I am asking for too much!

            // Loop through the menu items looking for placeholder(s)
            foreach ( $items as $item ) {

            // Is this the placeholder we’re looking for?
            if ( ‘#lastpost’ != $item->url )
            continue;

            $item->url = previous_posts( false );

            if ( empty( $latestpost ) )
            continue;

            // Replace the placeholder with the real URL
            $item->url = get_permalink( $latestpost[0]->ID );
            }

  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,
    ) );

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

    Then, it worked perfectly, thx 😉

    • It’s pretty straight-forward:

      // 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 ) {
      		if ( '#latestpost1' == $item->url ) {
      			$latestpost = get_posts( array(
      				'numberposts' => 1,
      			) );
      		}
      		elseif ( '#latestpost2' == $item->url ) {
      			$latestpost = get_posts( array(
      				'offset'      => 1,
      				'numberposts' => 1,
      			) );
      		}
      		elseif ( '#latestpost3' == $item->url ) {
      			$latestpost = get_posts( array(
      				'offset'      => 2,
      				'numberposts' => 1,
      			) );
      		}
      		else {
      			$latestpost = false;
      		}
      
      		if ( empty( $latestpost ) ) {
      			continue;
      		}
      
      		$item->url = get_permalink( $latestpost[0]->ID );
      	}
      
      	// Return the modified (or maybe unmodified) menu items array
      	return $items;
      }

      Extend from there if you want more. 🙂

  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 | T-machine.org

Leave a Reply