X

How to Add Date to a Specific wp_nav_menu

Snippets by IsItWP

Want to know how to add date to a specific wp_nav_menu()? It’s easy with our code snippet. In this article, you’ll learn how to add the date.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin

Don’t forget to change MENU_NAME to the name of the menu you wish to add the date.

add_filter('wp_nav_menu_items','add_date', 10, 2);
function add_date($items, $args) {
    if( $args->theme_location == 'MENU_NAME' )
        return $items . '<li class="navdate">' . date("l F jS, Y") . '</li>';
 
    return $items;
}

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 62 best free WordPress blog themes or 7 best WordPress contact form plugins.

Comments   Leave a Reply

  1. Should be added that the MENU_NAME is the one which the menu was registered with in functions.php not the one you set up in Admin.

  2. strange, the conditional seems to return false even if the menu name matches 

    1. Ill take a look,

  3. i use this technique to add a logout button to the nav if a user is logged in.

    1. Ahh cool that is a good way to add it. Good call,
      you should contribute a snippet or two http://wpsnipp.com/index.php/contribute/

  4. Where and how could one make use of such function?

    1. If you notice at the top of my site, I have the last update date displayed. You could use it for something like this or in fact you could use this snippet to insert anything into the wp_nav_menu, eg: number of twitter followers, or facebook fans, or a select menu and search field etc. really anything that you need to insert into the menu when you need more then just a link.

      1. I’ll save it then. It might be handy for sometime in the future 🙂

        1. or just visit wpsnipp in the future since I have it saved 🙂 don’t forget to follow us on twitter or facebook.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!