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.
Nice work Alex. Is it possible to just change line 20 to get a drop-down menu with multiple recent posts?
Adding menu items gets significantly more complicated. See the source of my Add Descendants As Submenu Items as an example.
An easier solution would be to just create more placeholders and have the code snippet handle those too, such as
#latestpost1
,#latestpost2
, etc.how can i use it for another menu item to a different category?
I have been looking for something similar to this, but how can I display last post’s title on the menu ?
First off, this is a fantastic snippet. Thanks for putting it out there.
Question: Is it possible to confine this snippet to drawing posts from a particular category?
Yes! See this snippet from the code?
Just add the category in there, like so:
Hope that helps! =)
Oops, I somehow overlooked Bri Bri’s comment. Thanks for the reply.
I also made a small fix to your code (wrong parameter name). 🙂
Duh copypasta! Thanks, Alex. And thanks for making it all code-pretty, too.
Finding this post helped me out, so helping someone else out (or, well, trying to) was the least I could do. =)
Hi, thanks, the code is working. Is there also a way to retrieve also the name of the post in the menu label?
After this:
Add this:
Thank you!!! Works like a charm!
Code worked perfect the first time in my menu. Thanks!
Is there a way to use this code to make a link from a button on a page to the most recent category? (Same way it does from the menu) I have been beating my head on my desk trying to figure out a solution on this one…
“A button on a page”? Such as in the content of the page? Sounds like you want a shortcode for that.
Thanks for this snipped Alex. works great on the menu.
I’m trying to redirect from the footer links (on a template) to the most recent post of a category, and the previous shortcodes doesn’t work unless it’s in a content page.
Any idea how to get that working?
Thanks for your time.
Alex
its working for me 🙂
Thank you so much!
what if i want to use it for another menu item for a different category?
What do you mean “a different category”? Show the latest post in a particular category? You’ll want extra placeholders or something along those lines.
I use your code for “Custom Link” menu item that links with the placeholder to last post of category id 4. And it’s working fine: (part of the code)
if ( ‘#latestpost’ != $item->url )
continue;
// Get the latest post
$latestpost = get_posts( array(
‘numberposts’ => 1,
‘category’ => 4,
) );
What I need is to use another “Custom Link” menu item, That will link to the latest post in category id 5.
What is the complete code for doing this?
This is what I sent him via e-mail: https://gist.github.com/2638975
This code is fantastic- what I’d like to know is how to have the 5 latest blog posts instead of just the first. Is that possible?
Well a menu item can only have a single link. 😉
The easiest solution would be something like this: https://gist.github.com/2638975
But instead of setting
$args['category']
instead set$args['offset']
to varying numbers to get the correct post for each placeholder.Thanks Viper, this helped me solve something that I could not fix with a plugin…
For anyone looking to do this with a custom post type, here’s the part of the code that you need to edit (add the ‘post_type” line):
$latestpost = get_posts( array(
‘numberposts’ => 1,
‘post_type’ => ‘your_post_type_here’,
) );
And to lookup all the other arguments that you can pass into the get_posts function, see:
http://codex.wordpress.org/Function_Reference/get_post
Thank you! very very helpful!
Hello. Thanks for the helpful code. I modified it slightly as suggested in the comments to return the latest 5 entries along with their titles. I would like to truncate long titles though so it works better in my menu. I have tried unsuccessful to modify the script to do so usually other tutorials a reference but it’s clear I don’t really know what I’m doing. Can someone help me out? Here’s what I have
Replace this line:
With this:
That will cut off the title before the first space found after the first 35 characters are skipped.
Thanks for the quick reply. For some reason I was getting an offset error so I just simplified it to
Would be nice to add a “…” at the end but I don’t want to get too complicated with ifs. Thanks
Would only want it to add the “…” if it actually did trim the post title down and not for every post title. Thanks.
Pingback: Adding Dynamic Product Lists To Your WordPress Menu | Chrome Orange
Code works great, but its just a plain page. How can I get the widgets to show on the latests post page
Sweet! thanks!
Hi Alex, I am trying to put together something similar, I need a menu item that dynamically links to the most recently added category of a custom taxonomy. Any ideas on how to pull that into the loop?
Thanks, worked like a charm!
Thanks a lot for this! 🙂
Hi Trixi, I could manage to do it with a plugin called “Blog in Blog”. It’s pretty straight forward:
As the documentation explains, use a shortcode in your page (mine e.g = [blog_in_blog category_slug=’brands+case-studies’ num=1 pagination=off] ). This calls the category brands and case studies and shows one post without pagination. Simply add the page ID to your menu.
Hope this helps! 😉
Alex
Hi
Love this, used it to create a login / logout link for WooCommerce https://gist.github.com/3596494
Thanks
This saved my bacon. Thanks Alex!
Thanks Alex, this code helped me a lot!
Now I want to have multiple menu items, all pointing to the latest posts within different categories. How do I duplicate this code in functions.php.
I want a button for the category ‘Life’, and for ‘Sketches’, and from what I’ve understood, I use ‘category’ => Life,
But do I duplicate the whole snippet of code multiple times for each category, or is there a better way of doing it?
Regards,
Øystein
Thank you for your code, which I found almost usefull to me 🙂 the issue here is that I need to add the latest 11 posts to my menu item number 45. Is it possible with this? Could you help? Thank you.
Best regards.
H.
I’ve got this help from WordPress Answers: http://wordpress.stackexchange.com/questions/103243/how-to-list-recent-posts-in-a-wp-nav-menu
This code worked great exactly what I was looking for… The shortcode example worked great for linking a button.
Hi, it works great.
How to make the menu item light up after using the post navigaion prev/next?
Thanks
this is brilliant, thank you.
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.
It’s no different than the front end of your site. The queries are fairly fast.
If you do want to cache them though, enable presistent object caching (say via memcached) and then run https://wordpress.org/plugins/advanced-caching/ which will cache WP_Query calls.
Mills… Please find my issue… i want to automatically add categories to different custom menus after filtering as submenus……
I am sorry, I do not understand your question. 🙁
After filtering categories i want it to be automatically populated to appropriate menu….please guide me….
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!
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()
returnsfalse
though, i.e. those functions won’t work when viewing your homepage.Random would just be
'orderby' => 'rand'
on theget_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.
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!
Just realized I put Sounders instead of Timbers…I was a Sounders fan until we got the Timbers. Old habits I guess.
Sorry, wrong function and you wouldn’t use
$latestpost
, just this:$item->url = previous_posts( false );
No idea if that’ll actually work or not…
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 );
}
Pingback: How to add a "Latest Post" link to my Wordpress nav menu
I had to replace
$latestpost = get_posts( array(
‘numberposts’ => 1,
) );
with:
$latestpost = get_posts( array(
‘posts_per_page’ => 1,
) );
Then, it worked perfectly, thx 😉
Brilliant, another problem solved! Thanks for taking the time to post.
Hi Alex,
Great stuff. Could you talk me through adding a second and third placeholder?
Many thanks!
It’s pretty straight-forward:
Extend from there if you want more. 🙂
Ah, thanks Alex,
I see what I was doing wrong now.
I neglected to mention that I wanted the different placeholders to be for different categories. So I simply removed the ‘offset’ and added category id (I’m assuming an offset of 2 shows the second most recent post?)
Thanks!
An offset of 2 actually skips 2 posts and displays the third post.
Thank you Alex!
This was what I needed!
Code Snippet: Add A Link To Latest Post To WordPress Nav Menu
It can be applied in last post portfolio?
thanks
Thanks! Works great.
it looks great! how can i define that the “latest post” would be from a specific post-type?
tnx 🙂
never mind, i got it and it works perfect 🙂
For anyone else with this question, make this change in the code:
// Get the latest post
$latestpost = get_posts( array(
‘numberposts’ => 1,
‘post_type’ => ‘whatever-post-type-you-want-goes-here’,
) );
or ‘category_name’ => ‘whatever-category-you-want-goes-here’
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?
You couldn’t. You’d need something different, probably a shortcode.
did you find a shortcode to do this? I’m trying to embed ‘latest blot post title and link’ on my home page but not in the primary navigation but haven’t found a solution.
Thanks for sharing.
Pingback: WordPress plugin: insert link to latest post (in category) on your menu | T-machine.org
Pingback: Link Roundup: March 2016 - WPVegas
Pingback: ostatni dodany post w menu - Wordpress Front End Developer
Hi Alex,
This blog is old, but working, even on latest WP. Nice work man 🙂
I have one question, maybe you can help. If I want to add a name/title of the blog post besides the URL, is there a way? How could I do that? I want to create dynamic megamenu with latest blog posts from few categories and I would like to fill them out without manual title editing.
Let me know if you can help.
Best,
Marin
Excellent! Thank you very much. This solved my problem and (still) works in the current version (4.9.1).
Hello, it works very well, but what I want to do is how can we show the latest additions to the relevant category under all categories?
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
Can we also show pictures?
tanq man !
Thanks dude for this very helpful piece of content!
Thank you Alex. This what I needed.
I wonder why there is no such option in WP.
Thanks for posting this tip. I did apply it but it doesn’t seem to be working. I just get the domainname/#latestposts url. I also updated the functions.php file. So I’m not sure what I’m doing wrong