Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Wordpress twentyten dynamic menu list child taxonomies WordPress



I've got a navigation bar using the wordpress twentyten dynamic menu.

In my navigation I am listing a mix of taxonomies and pages.

But on my taxonomies, I want the child taxonomies to be displayed in the format of a nested list. The markup needs to be similar format to when you nest elements in the wordpress menu editor.

This is so I can create a dropdown using css.

I think the answer is hidden here [[LINK href=""]][[/LINK]]

This is what ive tried...

<?php wp_nav_menu( array( 'container_class' => 'third-nav', 'theme_location' => 'menu-3', 'walker' => new Custom_Walker_Nav_Menu() )); ?>

with this function...


function register_nav_menu_class(){
class Custom_Walker_Nav_Menu 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;
$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 .= '</a>';

if($item->object == 'category'){

$child_cats = wp_list_categories('taxonomy=group&title_li=&echo=0&child_of='.$item->object_id);

if( $child_cats ){
$item_output .= '<ul class="sub-menu">' .$child_cats. '</ul>';;

$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );




But can't get any joy of it.

Person to post working code will get the prize. Thanks

Answers (2)


Ivaylo Draganov answers:


there's no need for a custom walker here. You can do it via a filter:
[[LINK href=""]][[/LINK]]

Of course a walker will do the trick as well. I can help you with that as well if you really need it.

Josh Cranwell comments:

Do I need to add anything to this?

<?php wp_nav_menu( array( 'container_class' => 'third-nav', 'theme_location' => 'menu-3' )); ?>

I've added your paste bin into my functions, but getting anything yet.


Ivaylo Draganov comments:

I noticed that you are checking for 'category' object type(taxonomy) but later querying for 'group' taxonomy:

if($item->object == 'category'){
$child_cats = wp_list_categories('taxonomy=group&title_li=&echo=0&child_of='.$item->object_id);

Is that intentional? If it's a mistake then that's what may be causing you trouble.

Ivaylo Draganov comments:

<blockquote>Do I need to add anything to this?</blockquote>

No, this is correct. What is the name of the taxonomy to which the terms belong? My example is for 'category' (the default ones). Change that to the name of your taxonomy. It's also possible to check for multiple taxonomies.

Josh Cranwell comments:

Even if I change 'category' to 'taxonomy', my child taxonomies are still not listing.

I'm not really sure what this walker idea is doing, I just want to be able to list child taxonomies in my menu?

If your filter will work then I will use that, but how do I connect your filter to my menu code below?

<?php wp_nav_menu( array( 'container_class' => 'third-nav', 'theme_location' => 'menu-3' )) ?>


Josh Cranwell comments:

It needs to be dynamic, so if I add a taxonomy later to my menu, then it automatically lists the child taxonomies, if there are any.

Ivaylo Draganov comments:

It will be dynamic and Wordpress auto-connects it - you only have to place the code in functions.php. I've pasted an updated version at the same URL. Now it handles all taxonomies without any changes. Give it a try:
[[LINK href=""]][[/LINK]]

Bear in mind that if there are no posts associated with a term it won't show up in the list. If you want to have empty terms listed, then uncomment the line that contains <em>'hide_empty' -> 0</em>

Josh Cranwell comments:


But the taxonomies with no children are displaying...

<ul class="children"><li>No categories</li></ul>

Anyway to return absolutely nothing if no children exist?


Ivaylo Draganov comments:

<blockquote>Anyway to return absolutely nothing if no children exist?</blockquote>
Yes, add this to the array of arguments:

'show_option_none' => '',

Or grab [[LINK href=""]]my latest code[[/LINK]].

Josh Cranwell comments:

Bingo :-) Thanks


Christianto answers:

Hi Josh,

Sorry because this is out of topic..
have you solved your problem about "Simple jQuery Fancybox to open different size image" on
[[LINK href=""]][[/LINK]]

What about my answer? did it work?