dropdown_menu(); with the same arguments as wp_nav_menu();
.
Author: Robert O'Rourke @ interconnect/it
Version: 0.5
Author URI: http://interconnectit.com
*/
/*
Changelog:
0.5:
improved backwards compat with getElementsByClassName. Works back to IE 5.5. Thanks to Rob Nyman http://code.google.com/p/getelementsbyclassname/
0.4:
added the use of the menu name as the blank item text
fixed it for when the menu object wasn't present if called via theme_location
changed white space to reflect coding guidelines
0.3:
added an argument to alter the blanking text, empty to not have it all together, and an improved filter that passes $args
changed widget class name
*/
// pretty useless without this
if ( ! function_exists( 'wp_nav_menu' ) )
return false;
/**
* Tack on the blank option for urls not in the menu
*/
add_filter( 'wp_nav_menu_items', 'dropdown_add_blank_item', 10, 2 );
function dropdown_add_blank_item( $items, $args ) {
if ( isset( $args->walker ) && is_object( $args->walker ) && method_exists( $args->walker, 'is_dropdown' ) ) {
if ( ( ! isset( $args->menu ) || empty( $args->menu ) ) && isset( $args->theme_location ) ) {
$theme_locations = get_nav_menu_locations();
$args->menu = wp_get_nav_menu_object( $theme_locations[ $args->theme_location ] );
}
$title = isset( $args->dropdown_title ) ? wptexturize( $args->dropdown_title ) : '— ' . $args->menu->name . ' —';
if ( ! empty( $title ) )
$items = '' . $items;
}
return $items;
}
/**
* Remove empty options created in the sub levels output
*/
add_filter( 'wp_nav_menu_items', 'dropdown_remove_empty_items', 10, 2 );
function dropdown_remove_empty_items( $items, $args ) {
if ( isset( $args->walker ) && is_object( $args->walker ) && method_exists( $args->walker, 'is_dropdown' ) )
$items = str_replace( "", "", $items );
return $items;
}
/**
* Script to make it go (no jquery! (for once))
*/
add_action( 'wp_footer', 'dropdown_javascript' );
function dropdown_javascript() {
if ( is_admin() ) return; ?>
%3$s';
// custom args for controlling indentation of sub menu items
$args[ 'indent_string' ] = isset( $args[ 'indent_string' ] ) ? $args[ 'indent_string' ] : '– ';
$args[ 'indent_after' ] = isset( $args[ 'indent_after' ] ) ? $args[ 'indent_after' ] : '';
wp_nav_menu( $args );
}
class DropDown_Nav_Menu extends Walker_Nav_Menu {
// easy way to check it's this walker we're using to mod the output
function is_dropdown() {
return true;
}
/**
* @see Walker::start_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
function start_lvl( &$output, $depth ) {
$output .= "";
}
/**
* @see Walker::end_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
function end_lvl( &$output, $depth ) {
$output .= "\n", $item, $depth);
}
}
/**
* Navigation DropDown Menu widget class
*/
class DropDown_Menu_Widget extends WP_Widget {
function __construct() {
$widget_ops = array( 'classname' => 'dropdown-menu-widget', 'description' => __( 'Use this widget to add one of your custom menus as a dropdown.', 'gdl_back_office') );
parent::__construct( 'dropdown_menu', __('Dropdown Menu', 'gdl_back_office'), $widget_ops );
}
function widget( $args, $instance ) {
// Get menu
$nav_menu = wp_get_nav_menu_object( $instance[ 'nav_menu' ] );
if ( ! $nav_menu )
return;
$instance[ 'title' ] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
echo $args[ 'before_widget' ];
if ( ! empty( $instance[ 'title' ] ) )
echo $args[ 'before_title' ] . $instance[ 'title' ] . $args[ 'after_title' ];
dropdown_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu ) );
echo $args[ 'after_widget' ];
}
function update( $new_instance, $old_instance ) {
$instance[ 'title' ] = strip_tags( stripslashes( $new_instance[ 'title' ] ) );
$instance[ 'nav_menu' ] = (int) $new_instance[ 'nav_menu' ];
return $instance;
}
function form( $instance ) {
$title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : '';
$nav_menu = isset( $instance[ 'nav_menu' ] ) ? $instance[ 'nav_menu' ] : '';
// Get menus
$menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
// If no menus exists, direct the user to go and create some.
if ( ! $menus ) {
echo '
'. sprintf( __( 'No menus have been created yet. Create some.' ), admin_url( 'nav-menus.php' ) ) .'
'; return; } ?>