• I hardocded a menu item to the WP menu, and rather than that menu item having a standard dropdown link, I need to have the dropdown include a widget area. I have almost everything figured out, but I’m not sure how to call the widget area within the menu item.

    Here’s the code for the menu item in the functions.php file:

    /* Add menu items to the PreHeader menu */
    function my_menu_items($items, $args) {
        if( $args->theme_location == 'preheader' ){
    		$items .= '<li><a class="dropup-link" href="#">More</a>WIDGET AREA NEEDS TO GO HERE</li>';
    		$items .= '<li><a href="#">Customer Login</a></li>';
        }
        return $items;
    }
    add_filter( 'wp_nav_menu_items', 'my_menu_items', 10, 2 );

    I need to include the widget area after the “More” link (as indicated in the note in the code above). How do I do that?

Viewing 2 replies - 1 through 2 (of 2 total)
  • HA! I just used your question to solve my own code dilemna so thank YOU! The least I could do is post my solution. Here’s a full example:

    /* Changes to your example */
    
    /* Add menu items to the PreHeader menu */
    function my_menu_items($items, $args) {
        if( $args->theme_location == 'preheader' ){
    		$items .= '<li><a class="dropup-link" href="#">More</a>'.do_action('my_widget_action').'</li>';
    		$items .= '<li><a href="#">Customer Login</a></li>';
        }
        return $items;
    }
    add_filter( 'wp_nav_menu_items', 'my_menu_items', 10, 2 );
    
    /* Add widgets to your theme (if you haven't already) */
    
    /* Define extra widget areas */
    function my_widgets_init() {
        register_sidebar(array(
           	'name' => 'Navbar',
           	'id' => 'navbar-widget',
           	'before_widget' => '',
           	'after_widget' => "",
    	'before_title' => '',
    	'after_title' => "",
        ));
    }
    add_action( 'init', 'my_widgets_init' );
    
    /* Add the navbar widget */
    function my_navbar_widget() {
          if (function_exists('dynamic_sidebar')) {
    	dynamic_sidebar('navbar-widget');
          }
    }
    add_action('my_widget_action', 'my_navbar_widget', 8);

    Hope it makes sense.

    EDIT:

    Had to do this to get the widget to render properly. Since the actions echo by default, I created a filter instead and buffered the output.

    /* Add menu items to the PreHeader menu */
    function my_menu_items($items, $args) {
        if( $args->theme_location == 'preheader' ){
    		$items .= '<li><a class="dropup-link" href="#">More</a>'.apply_filters('my_filter').'</li>';
    		$items .= '<li><a href="#">Customer Login</a></li>';
        }
        return $items;
    }
    add_filter( 'wp_nav_menu_items', 'my_menu_items', 10, 2 );
    
    /* Add widgets to your theme (if you haven't already) */
    
    /* Define extra widget areas */
    function my_widgets_init() {
        register_sidebar(array(
           	'name' => 'Navbar',
           	'id' => 'navbar-widget',
           	'before_widget' => '',
           	'after_widget' => "",
    	'before_title' => '',
    	'after_title' => "",
        ));
    }
    add_action( 'init', 'my_widgets_init' );
    
    /* Adding the navbar widget */
    function my_navbar_widget() {
          if (function_exists('dynamic_sidebar')) {
    	ob_start();
    	dynamic_sidebar('navbar-widget');
    	$out = ob_get_clean();
    	return $out;
          }
    }
    add_filter('my_filter', my_navbar_widget', 8);

    Seems to work so far.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add Widget Area to custom WP menu item?’ is closed to new replies.