Problem using output buffer within walker
-
I’m trying to use the output buffer to get the content for dynamic_sidebar() from within a menu walker. The result I just doesn’t place the content within the expected `<div>, but instead places it outside the <li> for the menu item. I just can’t make sense of it. If I perform the same buffering outside the start_el function, then I get the expected result, though of course not where I need it. Is there something going on here that I’m not aware of?
class Menu_With_Description extends Walker_Nav_Menu { function start_el( &$output, $item, $depth = 0, $args = array(), $current_object_id = 0) { global $wp_query; $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; $class_names = $value = ''; $classes = empty( $item->classes ) ? array() : (array) $item->classes; $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ); $class_names = ' class="' . esc_attr( $class_names ) . ' menu-level-' . $depth . '"'; $output .= $indent . '<li id="menu-item-'. $item->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>'; //$item_output .= '<span class="menu-description">' . $item->description . '</span>'; if ( is_active_sidebar( 'mega-menu-widget-area-' . $item->ID ) ) { $item_output .= "<div id=\"mega-menu-{$item->ID}\" class=\"mega-menu\">"; ob_start(); dynamic_sidebar( 'mega-menu-widget-area-' . $item->ID ); $item_output .= ob_get_clean(); $item_output .= "</div>"; } $item_output .= $args->after; $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); } }
[Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Problem using output buffer within walker’ is closed to new replies.