adding an additional link to the wp_pages_dropdown

2019-09-06 19:08发布

I'm working on a wordpress theme that uses dropdowns for part of the site navigation.

I'm using wp_pages_dropdown and wp_dropdown_categories to output my menus as a dropdown jump menu, this is all good,works fine, but I would like to add a 'home' link to the end of the list of <option>'s

I have been having a go at it - my html/css is spotless but I am not a programmer, can any one help me out, I'd really appreciate it.

<?php wp_dropdown_pages('show_option_none=Select Page');?>

<?php wp_dropdown_categories('show_option_none=Select Category'); ?>

I am thinking that I need to somehow work <?php echo get_option('home'); ?> into it somehow. Any suggestions? thanks.

2条回答
劳资没心,怎么记你
2楼-- · 2019-09-06 19:56

I'd say the simplest approach is to just filter the output of wp_dropdown_... and inject your option just before the closing select tag;

function insert_home_in_dropdown($output)
{
    $end = '<option value="whatever">Home</option></select>';
    return preg_replace('#</select>$#', $end, trim($output)); 
}
add_filter('wp_dropdown_pages', 'insert_home_in_dropdown');
查看更多
家丑人穷心不美
3楼-- · 2019-09-06 20:04

My code looks like this, and in my functions.php code I edited the value to be 0.

    var dropdownf = document.getElementById("page_id");
                function onPageChange() {
                if ( dropdownf.options[dropdownf.selectedIndex].value > 0 ) {
                location.href = "<?php echo get_option('home'); ?>/?page_id="+dropdownf.options[dropdownf.selectedIndex].value;
                    }

                if ( dropdownf.options[dropdownf.selectedIndex].value == 0 ) {
                location.href = "<?php echo get_option('home'); ?>"; // this give the href to the home link 
                    }

                }
                dropdownf.onchange = onPageChange;
查看更多
登录 后发表回答