WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Aug 23 2011

How to Get WordPress's Custom Menu Description

So recently, someone wanted to use menu descriptions as a tagline of sorts for a variety of pages, categories, archives, etc. instead of using basic metaboxes on pages (since this would also apply to category pages, etc.). So I wrote up a simple function that I believe may be helpful for others. So here is the code. To use, simply copy the function to your functions.php file and then use the function wherever you'd like.

[php]<?php
function wps_get_menu_description( $args = array() ) {
global $post;

// Default
$defaults = array(
'echo' => false,
'format' => '',
'description' => '',
'location' => 'primary',
'classes' => 'post-description'
);

$args = wp_parse_args( $args, $defaults );
extract( $args , EXTR_SKIP );

// Get menu
$menu_locations = get_nav_menu_locations();
$nav_items = wp_get_nav_menu_items( $menu_locations[ $location ] );

// Cycle through nav items
foreach ( $nav_items as $nav_item ) {

if ( ( is_page() || is_single() || is_archive() ) && ( $nav_item->object_id == $post->ID ) ) {
$description = $nav_item->description;
}
elseif ( ( is_category() ) && ( $nav_item->object == 'category' ) ) {
$cat = get_query_var( 'cat' );
if ( $nav_item->object_id == $cat )
$description = $nav_item->description;
}
}

// Get output formatting
if ( $format == 'html' )
$output = apply_filters( 'wps_get_menu_description_output' , '<div class="'. $classes .'">' . $description . '</div>' , $args );
else
$output = $description;

// Echo description
if ($echo)
echo $output;

// Return description
return $output;
}
[/php]

To call the function, you just set the args in an array like this:
wps_get_menu_description( array('echo' => true, 'format'=>'html', 'classes' => 'post-info') );

With most frameworks, like Genesis/Thesis, you may need to call it like this (example reflects Genesis):
[php]<?php
// Add and Customize a Tagline under the Page Title
add_action('genesis_after_post_title', 'my_tagline');
function my_tagline() {
wps_get_menu_description( array('echo' => true, 'format'=>'html', 'classes' => 'post-info') );
}[/php]

Word of Warning: Please check your descriptions, and do not assume that there is nothing there if you didn't put it there. Your theme or plugins may have inserted content there as I've seen the description contain the_content automagically.

Do you see anything that I missed? Is there something else that you wish was added?

Written by Travis Smith · Categorized: Tutorials, WordPress

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. David Decker says

    August 23, 2011 at 12:38 pm

    Hi there! I tried to use this tutorial but didn’t work – I placed the setup function after init in functions.php and below that the caller function but nothing happens. I guess I have to place the caller somewhere else but where? Any help appreciated! – I just want to place some menu description for main nav, like defined in regular WP menu system. Thank you!

    Reply
    • wpsmith says

      August 23, 2011 at 1:06 pm

      Hello David,

      If you are using Genesis, you still need to apply it to an add_action statement. So it would look something like this:
      [php]<?php
      // Add and Customize a Tagline under the Page Title
      add_action(‘genesis_after_post_title’, ‘my_tagline’);
      function my_tagline() {
      wps_get_menu_description( array(‘echo’ => true, ‘format’=>’html’, ‘classes’ => ‘post-info’) );
      }[/php]

      Reply
  2. David Decker says

    August 23, 2011 at 3:16 pm

    Thank you, works like a charm! Really cool way to display some additional info on some pages! Thanks for the nice tutorial and support! -Dave 🙂

    Reply
  3. Kirsten says

    October 20, 2011 at 12:54 pm

    I’m trying to use this in my footer, and have added function code to my functions.php file, but when I go to call it in my footer.php file, nothing happens. Can you explain how to pair this with the wp_nav_menu function to display a certain menu in the footer? Also, I noticed that if I add ‘location’ => ‘footer’ to the args when calling wps_get_menu_description I get an error. The way around this is to just call my footer menu “primary”, but it would be nice to be able to change the menu to which this gets applied.

    Any help would be greatly appreciated! Thanks!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in