WordPress plugin: insert link to latest post (in category) on your menu

Instructions:

  1. Copy/paste this into your functions.php (TODO: convert it to a standalone php file, and make it into a plygin you can activte/deactivate)
  2. Create a new menu item of type “custom URL”
  3. Make your URL “http://#latestpost:category_name”
    • where “category_name” is the name of the category whose latest post you want to link to
  4. Make the name whatver you want to appear on the menu
  5. Profit!

Based on an idea (with some upgrading + bugfixes for latest WordPress in 2016) from http://www.viper007bond.com/2011/09/20/code-snippet-add-a-link-to-latest-post-to-wordpress-nav-menu/

/** Adam: add support for putting 'latest post in category X' to menu: */
// 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 ) {

        $key = 'http://#latestpost:';

    // Loop through the menu items looking for placeholder(s)
    foreach ( $items as $item ) {
 
        // Is this the placeholder we're looking for?
        if ( 0 === strpos( $item->url, $key ) )
        {
 
        $catname = substr( $item->url, strlen($key) );
        // Get the latest post
        $latestpost = get_posts( array(
            'posts_per_page' => 1,
                'category_name' => $catname
        ) );

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

7 thoughts on “WordPress plugin: insert link to latest post (in category) on your menu

  1. adam Post author

    Should be possible, yes, but half the code will be different – you’ll have to find out the best way to run functions in the middle of webpages (make a custom shortcode, maybe?)

  2. Sebastian Pertsch

    Thanks for that!

    You better should sourround your snippet with
    if (! is_admin()) {}
    to avoid converting the pseudo-link in the menu.

    Otherwise the “real” link will be shown next time you open “nav-menus.php” and the dynamical link is gone.

  3. DrGonzo

    Hey there,
    this snippet is great! Is there also a way to insert links to latestpost not only withing menus? I’m trying to achieve linking to the latestpost within the editor on a certain page. Seems like that parameter won’t be replaced if not a menu item..

  4. adam Post author

    I’m sure there is, but I haven’t experimented with it myself.

    Off the top of my head: when I was researching this for menus, there were a lot more resources for how to insert-in-post, and plugins that did it for you. Menus were harder to get working.

  5. DrGonzo

    Hi Adam,
    hm, right now it’s kind of the other way. I find more menu related plugins that do this and only some plugins that display recent posts in a certain way but not how to simply link them. There should be a way to replace the paramater within links as well including $links
    Just couldn’t figure out how to replace them yet :)

Leave a Reply

Your email address will not be published. Required fields are marked *