首页 > 解决方案 > wp_nav_menu_items 添加自定义项目第二个,而不是最后一个

问题描述

我有这个功能可以将菜单项添加到我的菜单中......我的问题是,是否可以将它们放在菜单中的第二位而不是最后一个?

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

      }
       return $items;
}

add_filter( 'wp_nav_menu_items', 'ur_add_loginout_link', 10, 2 );

标签: phpwordpress

解决方案


是的,但是您必须使用经过稍微修改的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}";
        }
    }
}

将此代码放入文件中,然后将其包含在主题的 functions.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
) );

推荐阅读