Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is my first project involving wordpress and i've got stuck trying to create a custom walker for a footer menu.

I basically want to change the menu from the <ul> <li></li> </ul> structure to a <p> <span></span> </p> structure.

The menu and stuff is displaying file and I have been able to adjust the start_el and end_el functions without issue so I now get this:

<ul> <span>*link*</span> <span>*link*</span> </ul>

Which is obviously half way there, I have written the start_lvl() and end_lvl() tags as shown below but they are ignored by wordpress - nothing I put in them (even die()) is executed (which is a bit suspicious) but I know the class is being called because of the start_el() function.

function start_lvl(&$output, $depth = 0, $args = array()) {
    $output = "\n<p class=\"sub-menu\">\n";
}

function end_lvl(&$output, $depth = 0, $args = array()) {
    $output .= "\n</p>\n";
}

Does anyone know why this is happening - have I missed something really obvious!?

I am running the latest version of wordpress on apache with php 5.3.6 if it helps.

Thanks in advance,

Felix :)

share|improve this question
1  
Based on what you've shown all the obvious stuff looks correct. –  totels Nov 15 '12 at 23:23
    
Cross reference wordpress.org/support/topic/… –  Ian Dunn Feb 11 '13 at 21:05

3 Answers 3

Your my_extended_walker class is OK, but when you call the wp_nav_menu function use the items_wrap parameter.

wp_nav_menu( array( 'items_wrap' => '<p>%3$s</p>', 'walker'=>new my_extended_walker() ) );

The start_lvl is used for children elements.

share|improve this answer

Have you thought about fetching all of the navigation menu items using the built-in function: wp_get_nav_menu_items(); and manually outputting the contents?

Example :: Building simple menu list

    // Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu)
    // This code based on wp_nav_menu's code to get Menu ID from menu slug

    $menu_name = 'custom_menu_slug';

    if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
    $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );

    $menu_items = wp_get_nav_menu_items($menu->term_id);

    $menu_list = '<ul id="menu-' . $menu_name . '">';

    foreach ( (array) $menu_items as $key => $menu_item ) {
        $title = $menu_item->title;
        $url = $menu_item->url;
        $menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
    }
    $menu_list .= '</ul>';
    } else {
    $menu_list = '<ul><li>Menu "' . $menu_name . '" not defined.</li></ul>';
    }
    // $menu_list now ready to output
share|improve this answer

Ok, not sure what I read wrong when I posted this, but your whole class should be something like this:

class my_extended_walker extends Walker_Nav_Menu {
    function start_lvl(&$output, $depth = 0, $args = array()) {
        $output .= "\n<p class=\"sub-menu\">\n";
    }

    function end_lvl(&$output, $depth = 0, $args = array()) {
        $output .= "\n</p>\n";
    }
}

And then pass the 'walker' as a parameter when you call wp_nav_menu

wp_nav_menu( array( 'walker'=>new my_extended_walker() ) );

If you've got that you should be fine. I am looking at working code that does just that. I don't know about putting <p> tags in there though. You may get invalid markup.

share|improve this answer
    
Hi, yeah i've done that below is the full class, –  fenfe1 Nov 16 '12 at 8:16
    
class BB_Nav_Footer_Walker extends Walker_Nav_Menu { function start_lvl(&$output, $depth = 0, $args = array()) { $output = "\n<p class=\"sub-menu\">\n"; } function end_lvl(&$output, $depth = 0, $args = array()) { $output .= "\n</p>\n"; } } –  fenfe1 Nov 16 '12 at 8:17
    
I've tried using <div> instead of <p> just to see if I can get those functions to do anything but so far no joy :( –  fenfe1 Nov 16 '12 at 8:18
    
The <div> and <p> shouldn't break the Walker but might result in invalid markup. You have $output = and not $output .= in your start_lvl. That could be causing trouble. Is that the complete Walker you posted? –  s_ha_dum Nov 16 '12 at 14:22
    
Hi yeah I fixed the .= bit this is now the complete class (I removed the start_lvl() and end_lvl() functions to see if they made a difference) - pastebin.com/zfcBxHd8 –  fenfe1 Nov 17 '12 at 9:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.