• Resolved oliveira.taquari

    (@oliveirataquari)


    Is it possible to make the dropdown menu display the selected record after loading the new page?
    Otherwise the “Select” option will always be left first.
    Tank

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

    (@linux4me2)

    Hi @oliveirataquari,

    I’m trying to understand how this would be desirable, since the plugin is designed to provide menus in posts/pages to navigate to other posts/pages/urls where the dropdown would not be visible. Are you trying to use the Menu In Post (MIP) plugin to navigate to the same page the MIP dropdown is on; e.g., for named anchors on the same page or a widget?

    Plugin Author linux4me2

    (@linux4me2)

    I haven’t heard back from you, so I am going to assume you have resolved this. Please let me know if you have further questions.

    @oliveirataquari

    Solution is pretty simple:

    Make your function start_el in menu-in-post.php to looks like:

    function start_el(&$output, $item, $depth = 0, $args = null, $id = 0)
        {
    	// You need $wp
    	global $wp;
    		
            // Create each option.
            $item_output = '';
    
            // Add spacing to the title based on the depth.
            $item->title = str_repeat(' - ', $depth * 1) . $item->title;
    
            // Get the link.
            if (!empty($args->append_to_url)) {
                $attributes = !empty($item->url) ? ' value="' . esc_attr($item->url) . 
                    $args->append_to_url .'"' : '';
            } else {
                $attributes = !empty($item->url) ? ' value="' . esc_attr($item->url) . 
                    '"' : '';
            }
    	// Here you're setting selected attribute on option
    	$attributes.=' '.(home_url( $wp->request )==substr($item->url,0,-1) ? 'selected="selected"' : '');
    		
            // Add the HTML.
            $item_output .= '<option'. $attributes .'>';
            $item_output .= apply_filters('the_title_attribute', $item->title);
    
            // Add the new item to the output string.
            $output .= $item_output;
        }
    • This reply was modified 3 years, 8 months ago by kojotkk.

    And better one:
    $attributes.=' '.($item->current ? 'selected="selected"' : '');

    Plugin Author linux4me2

    (@linux4me2)

    @kojotkk Is this functionality you want for the plugin too, or are you just being helpful?

    Either way, thanks for the code suggestion.

    Plugin Author linux4me2

    (@linux4me2)

    I had to test the plugin for WP v. 5.8 and update the “tested to” version, so I went ahead and added this functionality to version 1.1.8 of Menu In Post which should be available for download momentarily. Thank you @oliveirataquari for the request and @kojotkk for the code snippet.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Dropdown menu display current page as first in list after selection’ is closed to new replies.