How to Add Menu Descriptions below menu links in your Wordpress website


How to Add Menu Descriptions in Your WordPress Themes
WordPress menu system has a built-in feature where you can add descriptions with menu items. However, this feature is hidden by default. Even when enabled, displaying them is not supported without adding some code. Most themes are not designed with menu-item descriptions in mind. In this article we will show you how to enable menu descriptions in WordPress and how to add menu descriptions in your WordPress themes.
Note: This tutorial requires you to have fair understanding of HTML, CSS, and WordPress theme development.
Before that, lets first talk a bit about why you would want to add menu descriptions? Well some people think that it will help with the SEO. But we think that the main reason why you would want to use them is to offer a better user experience on your site. Descriptions can be used to tell visitors what they will find if they clicked on a menu item. An intriguing description will attract more users to click on menus.
Menu Descriptions

Step 1: Turn on Menu Descriptions

Go to Appearance » Menus. Click on Screen Options button at top right corner of the page. Check the Descriptions box.
Enabling Menu Descriptions
This will enable descriptions field in your menu items. Like this:
Description field added to menu items
Now you can add menu descriptions to items in your WordPress menu. However, these descriptions will not yet appear in your themes. To display menu descriptions we will have to add some code.

Step 2: Add the walker class:

Walker class extends the existing class in WordPress. It basically just adds a line of code to display menu item descriptions. Add this code in your theme’sfunctions.php file.
01class Menu_With_Description extends Walker_Nav_Menu {
02    function start_el(&$output$item$depth$args) {
03        global $wp_query;
04        $indent = ( $depth ) ? str_repeat"\t"$depth ) : '';
05         
06        $class_names $value '';
07 
08        $classes empty$item->classes ) ? array() : (array)$item->classes;
09 
10        $class_names = join( ' ', apply_filters('nav_menu_css_class'array_filter$classes ), $item ) );
11        $class_names ' class="' . esc_attr( $class_names ) .'"';
12 
13        $output .= $indent '<li id="menu-item-'$item->ID .'"' $value $class_names .'>';
14 
15        $attributes = ! empty$item->attr_title ) ? ' title="'. esc_attr( $item->attr_title ) .'"' '';
16        $attributes .= ! empty$item->target ) ? ' target="' . esc_attr( $item->target ) .'"' '';
17        $attributes .= ! empty$item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' '';
18        $attributes .= ! empty$item->url ) ? ' href="' . esc_attr( $item->url ) .'"' '';
19 
20        $item_output $args->before;
21        $item_output .= '<a'$attributes .'>';
22        $item_output .= $args->link_before . apply_filters('the_title'$item->title, $item->ID ) . $args->link_after;
23        $item_output .= '<br /><span class="sub">' $item->description . '</span>';
24        $item_output .= '</a>';
25        $item_output .= $args->after;
26 
27        $output .= apply_filters( 'walker_nav_menu_start_el',$item_output$item$depth$args );
28    }
29}

Step 3. Enable Walker in wp_nav_menu

WordPress themes use wp_nav_menu() function to display menus. We have also published a tutorial for beginners about how to add custom navigation menus in WordPress Themes. Most WordPress themes add menus in header.php template. However, it is possible that your theme may have used some other template file to display menus.
What we need to do now is find wp_nav_menu() function in your theme (most likely in header.php) and change it like this.
1<?php $walker new Menu_With_Description; ?>
2 
3<?php wp_nav_menu( array'theme_location' => 'primary','menu_class' => 'nav-menu''walker' => $walker ) ); ?>
In the first line we set $walker to use walker class we defined earlier infunctions.php. In the second line of code, the only extra argument we need to add to our existing wp_nav_menu arguments is 'walker' => $walker.

To change how your descriptions appear on your site, you can add CSS in your theme’s stylesheet. We were testing this on Twenty Twelve and used this css.
1.menu-item {
2border-left1px solid #ccc;
3}
4 
5span.sub {
6font-style:italic;
7font-size:small;
8}
We hope that you will find this article useful and it will help you create cool looking menus with menu descriptions in your theme. Questions? Leave them in comments below

No comments:

Post a Comment