星期三 , 22 1 月 2025

wp导航nav walker

<?php class Custom_Nav_Walker extends Walker_Nav_Menu {
    function start_lvl( &$output, $depth = 0, $args = null ) {
        $output .= '<ul class="dropdown-menu nav_small" role="menu">';
    }

    function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
        $output .= '<li';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        if (in_array('menu-item-has-children', $classes)) {
            $classes[] = 'dropdown';
        }

        if (!empty($classes)) {
            $output .= ' class="' . esc_attr(implode(' ', $classes)) . '"';
        }

        $output .= '>';
        $output .= '<a href="' . $item->url . '"';
        
        if (in_array('active', $classes)) {
            $output .= ' class="active"';
        }

        if (in_array('dropdown-toggle', $classes)) {
            $output .= ' id="app_menudown"';
        }

        $output .= '>' . $item->title;

        if (in_array('menu-item-has-children', $classes)) {
            $output .= '<span class="glyphicon glyphicon-menu-down btn-xs"></span>';
            $output .= '</a>'; // Close the <a> tag
            $output .= '<a id="app_menudown" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><span class="glyphicon glyphicon-menu-down btn-xs"></span></a>'; // Output the dropdown toggle
            $output .= '<ul class="dropdown-menu nav_small" role="menu" style="display: none;">'; // Start the child menu
        } else {
            $output .= '</a>'; // Close the <a> tag
        }
    }

    function end_el( &$output, $item, $depth = 0, $args = null ) {
        $output .= '</li>';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        if (in_array('menu-item-has-children', $classes)) {
            $output .= '</ul>'; // Close the child menu
        }
    }
}
 ?>