Support » Plugin: Menu In Post » Dropdown For Child Menu Links

  • Resolved kingfisher64

    (@kingfisher64)


    Hello,

    Thanks for the plugin, it works great. I wondered if you would consider adding an option for menus with child links to convert to a dropdown?

    The use case would be if there are many links and a dropdown is more functional.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author linux4me2

    (@linux4me2)

    I’m glad you like it.

    The dropdown is an interesting idea. I’ll take a look at the possibilities.

    Thread Starter kingfisher64

    (@kingfisher64)

    Super stuff. I asked a dev friend of mine and he wrote something.

    All you have to do is change “menu_in_post_menu” TO “menu_in_post_dropdown” in the shortcode

    // ================================ START ================================
    
    add_shortcode('menu_in_post_dropdown', 'menu_in_post_output_dropdown');
    function menu_in_post_output_dropdown($atts = array(), $content, $shortcode_tag) {
    	if (isset($atts['menu'])) {
    		$menu = absint($atts['menu']);
    	} else {
    		$menu = 0;
    	}
    	if ($menu == 0) {
    		return menu_in_post_fallback_fxn();
    	} else {
    		$args = array('menu'=>$menu, 'fallback_cb'=>'menu_in_post_fallback_fxn', 'echo'=>false);
    	}
    	// If menu_id is empty, don't pass a value, and the menu slug with an incremented value added will be used. 
    	// If container_class is empty, don't pass a value and 'menu-{menu slug}-container' will be used.
    	$defaults = array(
    			'menu_class'=>'menu',
    			'menu_id'=>'',	
    			'container'=>'div', 
    			'container_class'=>'', 
    			'container_id'=>'', 
    			'depth'=>0 
    		);
    	$search = array('<script>', '</script>', '<?php', '?>');
    	foreach ($defaults as $att=>$default) {
    		switch($att) {
    			case 'depth':
    				if (isset($atts[$att])) {
    					$passed_depth = absint($atts[$att]);
    					if ($passed_depth > 0) {
    						$args['depth'] = $passed_depth;
    					}
    				}
    			break;
    			// These should be only strings.
    			default:
    				if (isset($atts[$att])) {
    					$passed_att = filter_var($atts[$att], FILTER_SANITIZE_STRING);
    					if ($passed_att != '') {
    						$args[$att] = $passed_att;
    					}
    				}
    		}	
    	}
    
    	return print_menu_shortcode($args, $atts['menu']);
    }
    
    function print_menu_shortcode($atts, $menuId) {
    	extract( shortcode_atts(
    		array(
    			'name' => null, 
    			'class' => null
    		), 
    		$atts
    	));
    
    	// Assuming $name contains slug or name of menue
    	$menu_items = wp_get_nav_menu_items($menuId); 
    
    	// Sample Output. Adjsut as per your exact requirements.
    
    	// Sample Output variable.
    	$menu_dropdown  = '';
    
    	if ($menu_items){
    		$menu_dropdown  .= '<select onChange="document.location.href=this.options[this.selectedIndex].value;">';
    
    		foreach( $menu_items as $menu_item ) {
    			$link = $menu_item->url;
    			$title = $menu_item->title;
    			$menu_dropdown  .= '<option value="' . $link .'">'. $title . '</option>' ;
    		}
    
    		$menu_dropdown  .= '</select>';
    
    	} else {
    	$menu_dropdown  = '<!-- no menu defined in location "'.$theme_location.'" -->';
    	}
    
    	return $menu_dropdown ;
    }
    
    // ================================ END ================================

    https://prnt.sc/qo54y7 – Shows it in action.

    I just put # for container and stacked in wordpress menus as you normally do.

    Maybe this is helpful?

    • This reply was modified 1 year, 10 months ago by kingfisher64. Reason: code highighting
    • This reply was modified 1 year, 10 months ago by kingfisher64.
    Thread Starter kingfisher64

    (@kingfisher64)

    Should have said for the benefit of others who might read that it goes in functions.php

    Plugin Author linux4me2

    (@linux4me2)

    I added the dropdown functionality but for some reason subversion failed to include some necessary javascript files. Working on it…

    Plugin Author linux4me2

    (@linux4me2)

    I think I have that little issue with subversion straightened out with version 1.1.1, and all the necessary files are there now. I added backward-compatibility in v. 1.1.2, so that’s the current, stable version.

    I could think of two options to provide a dropdown option:

    1. Use CSS to style the unordered list as a dropdown
    2. Add some javascript to navigate to the page using a select element

    I think the CSS approach is too cumbersome and likely to cause too many theme conflicts, so I went with option two. You select the menu style using the Menu In Post Shortcode Builder, or you can add:

    
    style="dropdown"
    

    to an existing shortcode to change the style to dropdown.

    Please give it a whirl and let me know what you think.

    Thread Starter kingfisher64

    (@kingfisher64)

    Works great for me!

    Super useful the idea that a person can create the menu in the backend of the site as they need no expertise to add/edit code.

    Can just create a menu link and paste that in. Anyone can do that.

    Did you end up using the code? I’ll tell my friend if you did.

    Plugin Author linux4me2

    (@linux4me2)

    Great! I’ll mark this as resolved.

    No, I appreciate it, but I didn’t use the code. I created a custom walker class to use with wp_nav_menu(), which allows you to pass the walker to build the menu. Same result, but a little more WordPress-ey, and allows the use of both the list and dropdown with the same code, basically.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Dropdown For Child Menu Links’ is closed to new replies.