wp_nav_menu_items添加自定义项目2,而不是最后一个

时间:2019-02-05 16:00:45

标签: php wordpress

我具有此功能,可将菜单项添加到菜单中...我的问题是,是否有可能使菜单项位于菜单的第二位而不是最后一位?

node_modules

1 个答案:

答案 0 :(得分:0)

是的,但是您必须使用Walker_Nav_Menu类的稍微修改的版本。

类似这样的东西:

class Custom_Menu_Walker extends Walker_Nav_Menu {
    private $counter;

    public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
            $t = '';
            $n = '';
        } else {
            $t = "\t";
            $n = "\n";
        }
        $indent = ( $depth ) ? str_repeat( $t, $depth ) : '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;

        $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

        $output .= $indent . '<li' . $id . $class_names .'>';

        $atts = array();
        $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
        $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
        $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
        $atts['href']   = ! empty( $item->url )        ? $item->url        : '';

        $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );

        $attributes = '';
        foreach ( $atts as $attr => $value ) {
            if ( ! empty( $value ) ) {
                $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
                $attributes .= ' ' . $attr . '="' . $value . '"';
            }
        }

        $title = apply_filters( 'the_title', $item->title, $item->ID );

        $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );

        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
        $item_output .= $args->link_before . $title . $args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }

    public function end_el( &$output, $item, $depth = 0, $args = array() ) {
        if ( $item->menu_item_parent === '0' ) {
            $this->counter++;
        }

        if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
            $t = '';
            $n = '';
        } else {
            $t = "\t";
            $n = "\n";
        }

        if ( ( $item->menu_item_parent === '0' ) && ( $this->counter === 1 ) ) {
            $output .= "</li>{$n}";

            if ( is_user_logged_in() && $args->theme_location == 'primary' ) {
                $output .= '<li><a href="'. ur_logout_url( get_permalink( ur_get_page_id( 'myaccount' ) ) ) .'">Blog Log Out</a></li>{$n}';
            } elseif ( ! is_user_logged_in() && $args->theme_location == 'primary' ) {
                $output .= '<li><a href="' . get_permalink( ur_get_page_id( 'registration' ) ) . '">Blog Registration</a></li>{$n}';
                $output .= '<li><a href="' . get_permalink( ur_get_page_id( 'myaccount' ) ) . '">Blog Log In</a></li>{$n}';
            }
        } else {
            $output .= "</li>{$n}";
        }
    }
}

将此代码放在文件中,然后将其包含在主题的function.php中。

然后,您必须在输出菜单的任何地方添加walker参数:

// Call the primary navigation menu

wp_nav_menu( array(
    'theme_location'    => 'primary', // Location
    'container'         => 'nav', // Set the container html tag
    'container_class'   => 'nav', // Set the class of the container or set to false if you dont need a class specified
    'walker'            => new Custom_Menu_Walker(), // Make use of the walker
) );