1

I've created a new navigation bar on this site: http://www.tomorrowstrends.com/

I'd like to add the date on the right hand side (on the grey menu below the blue one).

I thought I could add this to my new navigation php file (thirdnav.php):

<?php echo '<div class="date"<div>'; date(get_option('date_format')); ?>

So I would have this:

<?php 
if ( has_nav_menu( 'thirdnav' ) ) {
echo '<div id="subnav"><div>';  
wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_id' => 'thirdnav' , 'menu_class' => 'menu thirdnav superfish sf-js-enabled', 'theme_location' => 'thirdnav') ); 
echo '</div></div>'; 
} ?>

<?php echo '<div class="date"<div>'; date(get_option('date_format')); ?>

But I need to style it and I don't know the proper syntax for creating a class for the date within the echo statement.

How is that done? Am I doing this correctly?

Thanks in advance for any help.

1
  • 2
    Please add an image illustrating your desired output. A question should not rely on an external site.
    – fuxia
    Jul 17 '13 at 5:22
1

It would be better to hook into the wp_nav_menu_{$menu_name}_items filter and add your date item in there:

add_action( 'init', 'wpse_106781_add_menu_items_hook' );

function wpse_106781_add_menu_items_hook() {
    $theme_location  = 'thirdnav';
    $theme_locations = get_nav_menu_locations();

    /* Make sure the theme location is valid */
    if ( ! isset( $theme_locations[ $theme_location ] ) )
        return;

    /* Get the navigation menu assigned to that theme location */
    $menu = get_term( $theme_locations[ $theme_location ], 'nav_menu' );

    /* Make sure the menu is valid */
    if ( ! isset( $menu->slug ) )
        return;

    /* Add the filter hook */
    add_filter( "wp_nav_menu_{$menu->slug}_items", 'wpse_106781_thirdnav_menu_items' );
}

function wpse_106781_thirdnav_menu_items( $items ) {
    $items .= sprintf(
        '<li class="navmenu-date"><time datetime="%s">%s</time></li>',
        date( 'Y-m-d' ),
        date( get_option( 'date_format' ) )
    );
    return $items;
};

As this function affects the front-end appearance, it would be appropriate to place it in the theme's functions.php file.

Not only will it add the current date to the thirdnav menu, it will also use the appropriate HTML5 <time> element to represent the date. You can use the wp_nav_menu() function in your templates as normal.

You can style this item by using the class that is between <li class=" and "><time - in this case it is thirdnav-date, but it can be any valid HTML class you choose.

12
  • Okay, thank you. When I try to place this in functions.php I get a parse error. Is there some syntax I have to add or specific location in the code to put it?
    – user35399
    Jul 17 '13 at 21:58
  • @user35399 You might be using an old version of PHP - I've updated the code so it should work now. It needs to go in functions.php after the <?php and before the ?> (if there is one). If you'd rather not mess around with the functions.php file, you can just copy-and-paste it into the code snippets plugin.
    – shea
    Jul 18 '13 at 0:31
  • I've edited your code -- I think the parse error mentioned by @user35399 could be due to the original spritf() and/or a missing comma before the first call to date().
    – Pat J
    Jul 18 '13 at 3:17
  • I put in the code but the date is not showing up. Is there something else I have to do?
    – user35399
    Jul 20 '13 at 3:03
  • @user35399 I just tested this, and you're right. It turns out that you can't add items to a menu by it's theme location, you need to use the menu-specific slug. I've added a function to retrieve the menu slug from the theme location. The updated code will work now.
    – shea
    Jul 20 '13 at 3:40

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.