自定义漫游下拉菜单显示当前页面

时间:2014-03-18 作者:user49109

我有这个下拉菜单,当我转到带有这个下拉菜单的页面时,我希望当前页面显示在下拉菜单中。我在文档中搜索过,但没有找到任何。

class Walker_Nav_Menu_Dropdown extends Walker_Nav_Menu {

    function start_lvl($output, $depth) {    }

    function end_lvl($output, $depth) {    }

    function start_el($output, $item, $depth, $args) {
        // Here is where we create each option.
        $item_output = \'\';

        // add spacing to the title based on the depth
        $item->title = str_repeat(" ", $depth * 4) . $item->title;

        // Get the attributes.. Though we likely don\'t need them for this...
        $attributes  = ! empty( $item->attr_title ) ? \' title="\'  . esc_attr( $item->attr_title ) .\'"\' : \'\';
        $attributes .= ! empty( $item->target )     ? \' target="\' . esc_attr( $item->target     ) .\'"\' : \'\';
        $attributes .= ! empty( $item->xfn )        ? \' rel="\'    . esc_attr( $item->xfn        ) .\'"\' : \'\';
        $attributes .= ! empty( $item->url )        ? \' value="\'   . esc_attr( $item->url        ) .\'"\' : \'\';

        // Add the html
        $item_output .= \'<option\'. $attributes .\'>\';
        $item_output .= apply_filters( \'the_title_attribute\', $item->title );

        // Add this new item to the output string.
        $output .= $item_output;

    }

    function end_el($output, $item, $depth) {
        // Close the item.
        $output .= "</option>\\n";
    }

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

Wordpress为活动菜单项添加“当前菜单项”类,您可以检查是否设置了该类,以及是否选择了该项的设置选项。

class Walker_Nav_Menu_Dropdown extends Walker_Nav_Menu {

    function start_lvl($output, $depth) {    }

    function end_lvl($output, $depth) {    }

    function start_el($output, $item, $depth, $args) {
        // Here is where we create each option.
        $item_output = \'\';

        // add spacing to the title based on the depth
        $item->title = str_repeat("&amp;nbsp;", $depth * 4) . $item->title;

        // Get the attributes.. Though we likely don\'t need them for this...
        $attributes  = ! empty( $item->attr_title ) ? \' title="\'  . esc_attr( $item->attr_title ) .\'"\' : \'\';
        $attributes .= ! empty( $item->target )     ? \' target="\' . esc_attr( $item->target     ) .\'"\' : \'\';
        $attributes .= ! empty( $item->xfn )        ? \' rel="\'    . esc_attr( $item->xfn        ) .\'"\' : \'\';
        $attributes .= ! empty( $item->url )        ? \' value="\'   . esc_attr( $item->url        ) .\'"\' : \'\';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes; 
        if ( in_array( \'current-menu-item\', $classes ) ) {
             $attributes .= \' selected\';
        }

        // Add the html
        $item_output .= \'<option\'. $attributes .\'>\';
        $item_output .= apply_filters( \'the_title_attribute\', $item->title );

        // Add this new item to the output string.
        $output .= $item_output;

    }

    function end_el($output, $item, $depth) {
        // Close the item.
        $output .= "</option>\\n";
    }

结束