8

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 :)

2

3 Answers 3

8

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.

1
  • 2
    The start_lvl is used for children elements. you just saved a lot of things in my life
    – ashutosh
    Commented Feb 17, 2016 at 12:40
3

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.

5
  • Hi, yeah i've done that below is the full class,
    – fenfe1
    Commented Nov 16, 2012 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
    Commented Nov 16, 2012 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
    Commented Nov 16, 2012 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
    Commented Nov 16, 2012 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
    Commented Nov 17, 2012 at 9:05
3
+25

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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