← Back to blog

Data Label on WordPress Navigation

  • Tutorials
  • 3:06 reading time (ish)
  • 590 words

Hey guys, I thought this might be of interest – nothing more than a code dump. If you have any questions or suggestions please leave a comment below!

This code snippet allows you to add data-labels to your WordPress (wp_nav_menu) navigation items. Why would you want to do this? In my particular case, I’m using it as a ‘hover state’  for a client build – When the user hovers over a menu item, the data-label scrolls in as the menu item scrolls out creating a nice little effect.

Just a heads up, this code was made on a random ‘light bulb’ moment so the code could do with some cleaning up! (I’ll get round to it eventually ;-))

HTML:

<ul id="mainMenu">
        <li class="menu-item current_page_item">
                <a href="URL HERE">
                        <span data-label="Home">
                                Home
                        </span>
                </a>
        </li>
</ul>

CSS:

nav ul li a span {
    display: inline-block;
    position: relative;
    -webkit-transition: -webkit-transform .3s ease;
    -moz-transition: -moz-transform .3s ease;
    -o-transition: -o-transform .3s ease;
    -ms-transition: -ms-transform .3s ease;
    transition: transform .3s ease;
}

nav ul li a span:after {
    content: attr(data-label);
    display: block;
    position: absolute;
    width: 100%;
    top: 100%;
    left: 0;
    opacity: 0;
    color: #fff;
    transition: opacity .3s ease;
    -webkit-transition: opacity .3s ease;
    -moz-transition: opacity .3s ease;
    -o-transition: opacity .3s ease;
    -ms-transition: opacity .3s ease;
}

nav ul li:hover a span:after {
    opacity: 1;
}

nav ul li:hover a span {
    -webkit-transform: translate(0px,-100%);
    -moz-transform: translate(0px,-100%);
    -o-transform: translate(0px,-100%);
    -ms-transform: translate(0px,-100%);
    transform: translate(0px,-100%);
}

Add the following into your functions.php

class description_walker extends Walker_Nav_Menu {
    function start_el(&$output, $item, $depth, $args) {
        global $wp_query;
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
 
        $class_names = $value = '';
 
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
 
        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
        $class_names = ' class="'. esc_attr( $class_names ) . '"';
 
        $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
 
        $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
        $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
 
        if($depth != 0) {
            $description = $append = $prepend = "";
        }
 
        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
	$item_output .= '<span data-label="'.esc_attr( $item->description ) .'">';
        $item_output .= $args->link_before .apply_filters( 'the_title', $item->title, $item->ID );
	$item_output .= '</span>';
        $item_output .= '</a>';
        $item_output .= $args->after;
 
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

Add this into your header.php

<?php

$defaults = array(
    'theme_location'  => '',
    'container'       => 'div',
    'menu_class'      => 'menu',
    'echo'            => true,
    'fallback_cb'     => 'wp_page_menu',
    'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
    'depth'           => 0,
    'walker' 	      => new description_walker()
);

wp_nav_menu( $defaults );

?>

Next, just add a ‘Description’ to each Menu Item via the Appearance > Menu admin page (if you can’t see the option, look top-right, toggle ‘Screen options’ and check ‘Description’).

Voila!

Thoughts? Comments?