ThemeShaper Forums » Thematic

[closed]

Adding subtitle to menu 3.0 links

(13 posts)
  • Started 3 years ago by helgatheviking
  • Latest reply from nervous
  • This topic is resolved

Tags:

  1. http://themeshaper.com/wordpress-menu-tricks/ shows how to do this for wp_page_menu. i was wondering if anyone had successfully achieved this w/ a 3.0 menu?

    justin tadlock hints at the capability of menu "descriptions" in the "What the menu system offers" section of his post:
    http://justintadlock.com/archives/2010/06/01/goodbye-headaches-hello-menus

    but only says that you have to write a custom function for it... and there is no mention that i can find in the codex.

    Posted 3 years ago #
  2. ok i've figured out half of it. to display the info you need to define a custom walker and include that in your menu arguments

    class Custom_Walker extends Walker_Nav_Menu {
    	/**
    	 * @see Walker::$tree_type
    	 * @since 3.0.0
    	 * @var string
    	 */
    	var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
    
    	/**
    	 * @see Walker::$db_fields
    	 * @since 3.0.0
    	 * @todo Decouple this.
    	 * @var array
    	 */
    	var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
    
    	/**
    	 * @see Walker::start_lvl()
    	 * @since 3.0.0
    	 *
    	 * @param string $output Passed by reference. Used to append additional content.
    	 * @param int $depth Depth of page. Used for padding.
    	 */
    	function start_lvl(&$output, $depth) {
    		$indent = str_repeat("\t", $depth);
    		$output .= "\n$indent<ul class=\"sub-menu\">\n";
    	}
    
    	/**
    	 * @see Walker::end_lvl()
    	 * @since 3.0.0
    	 *
    	 * @param string $output Passed by reference. Used to append additional content.
    	 * @param int $depth Depth of page. Used for padding.
    	 */
    	function end_lvl(&$output, $depth) {
    		$indent = str_repeat("\t", $depth);
    		$output .= "$indent</ul>\n";
    	}
    
    	/**
    	 * @see Walker::start_el()
    	 * @since 3.0.0
    	 *
    	 * @param string $output Passed by reference. Used to append additional content.
    	 * @param object $item Menu item data object.
    	 * @param int $depth Depth of menu item. Used for padding.
    	 * @param int $current_page Menu item ID.
    	 * @param object $args
    	 */
    	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;
    		$classes[] = 'menu-item-' . $item->ID;
    
    		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
    		$class_names = ' class="' . esc_attr( $class_names ) . '"';
    
    		$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
    		$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
    
    		$output .= $indent . '<li' . $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        ) .'"' : '';
    
    		$item_output = $args->before;
    		$item_output .= '<a'. $attributes .'>';
    		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    		$item_output .= ! empty( $item->description ) ? '<span>' . esc_attr( $item->description ) . '</span>' : '';
    		$item_output .= '</a>';
    		$item_output .= $args->after;
    
    		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    	}
    
    	/**
    	 * @see Walker::end_el()
    	 * @since 3.0.0
    	 *
    	 * @param string $output Passed by reference. Used to append additional content.
    	 * @param object $item Page data object. Not used.
    	 * @param int $depth Depth of page. Not Used.
    	 */
    	function end_el(&$output, $item, $depth) {
    		$output .= "</li>\n";
    	}
    }

    and the arguments:

    wp_nav_menu( array(  'walker' => new Custom_Walker, 	) );

    however, my problem is in editing the Description in the backend. in wp-admin/js/nav-menu.dev.js you can see that there is a function that adds a class of "hidden-field" to the advanced fields like "class" and "description", and then they are hidden w/ CSS, display: none.

    so now it would seem i need to figure out how to toggle which items get hidden and which i'd like to display.

    Posted 3 years ago #
  3. turns out i was over-complicating things. on the menus page, at the top-right of the screen there is "screen options". clicking on then and then you can check and/or uncheck the options you would like to be shown... which include descriptions and custom classes. i guess i will have to amend my menu mega thread w/ this new discovery.

    Posted 3 years ago #
  4. petergus
    Member

    im a bit confused on this because i see that description field is there, but "The description will be displayed in the menu if the current theme supports it.". so do we need to add something extra to get thematic to support this?

    Posted 2 years ago #
  5. yes, see my mega menu thread:
    http://forums.themeshaper.com/topic/thematic-menus-demystified-1

    Posted 2 years ago #
  6. petergus
    Member

    Thanks! (ill post here to stay more on topic)
    this code is doing interesting things... probably be cause im simply not doing something write.
    its putting itself directly before the body tag (even showing in the admin panel), and then taking the name of the main menu, 'menu-primary' and renaming the original primary menu to 'menu-primary-1'

    confused by it quite alot! and hints appreciated!
    Thanks!

    Posted 2 years ago #
  7. petergus
    Member

    probably because i dont know where to put this string :D
    wp_nav_menu( array( 'walker' => new Custom_Walker, ) );

    Posted 2 years ago #
  8. the part 4. filter the menu arguments of the mega menu thread. you filter in the new arguments.

    Posted 2 years ago #
  9. petergus
    Member

    right.
    something like this doesn't work...just no descriptions showing up.

    am i calling it wrong?
    (ive just copied the code you used on the walker above)

    //change menu arguments
    function menu_args( $args ) {
    $args = array( 'menu_class' => 'sf-menu', //default class that will apply default styles
    'container_class' => 'menu', //default class
    'depth' => 2, //limit the drop downs to only 2nd level

    'walker' => new Custom_Walker,

    );

    return $args;
    }
    add_filter( 'thematic_nav_menu_args', 'menu_args' );

    thanks alot for your tips! ...im new to theming as is obvious ;)

    Posted 2 years ago #
  10. petergus
    Member

    well, again using a straight copy of your walker code,
    and this this -

    //change menu arguments
    function menu_args( $args ) {
    $args = array('walker' => new Custom_Walker,
    	);
    return $args;
    }
    add_filter( 'wp_nav_menu', 'menu_args' );

    and it outputs all the menu simply with "Array"! funny, but, ok my php skills are just to low to realize how to tweak this, i would really appreciate the tips here :)

    seems like something like this should be bundled into the themes since its in the menu options...

    Posted 2 years ago #
  11. I managed to get this going with the below code:

    function my_wp_nav_menu_args( $args = '' )
    {
    	$args['walker'] = new Custom_Walker;
    	return $args;
    } // function
    
    add_filter( 'thematic_nav_menu_args', 'my_wp_nav_menu_args' );

    I was able to keep everything (the walker code and the above)in my child theme's function.php which was the struggle for me. Found the the solution by modifying an example in http://codex.wordpress.org/Function_Reference/wp_nav_menu

    Posted 2 years ago #
  12. doesn't setting

    $args = ''

    nullify any other args from the filter? i guess wp_nav_menu has defaults though so it probably doesn't matter.

    Posted 2 years ago #
  13. I didn't see any noticeable changes. It was stripping out the other $args when I tried petergus's method or if I try:

    <?php wp_nav_menu( array( 'walker' => 'new Custom_walker' ) ); ?>

    I am still getting a handle on php and thematic but I am guessing it has to do with the fact I am filtering 'thematic_nav_menu_args' and not dealing with wp_nav_menu directly... which is perhaps what you are saying about wp_nav_menu defaults now that I think about it.

    Posted 2 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.