My client would like to have a link to the most recent post in the main navigation, but still be able to manage the navigation through the dashboard as usual. I did not find a plugin that will do this task, so I may need to custom code it. I could add it into the child theme, but I don't think it would should up in the dashboard menu where they could move it to a different order, if they want.

My searches are not yielding results in either Google or this Community, but I may just not be using the correct search term.


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.

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

// 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;
  • I ran into this code in my searches, but it was unclear whether it would work to give my client dashboard functions. I will give it a try! Oct 19 '20 at 0:08
  • This works very well! Oct 30 '20 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.