繁体   English   中英

WordPress wp_nav_menu导航> a

[英]Wordpress wp_nav_menu nav > a

我实际上是在网站上工作,并且开发了主题。

wp_nav_menu有问题。

我有这样的导航:

<nav> 
   <a href="#">item 1</a>
   <a href="#">item 2</a>
   <a href="#">item 3</a>
   <a href="#">item 4</a>
</nav>

我想要这个菜单:

<nav> 
   <a href="#" class="one columns">item 1</a>
   <a href="#" class="two columns">item 2</a>
   <a href="#" class="two columns">item 2</a>
   <a href="#" class="one columns">item 1</a>
</nav>

如果愿意,可以为每个添加一个自定义类。 这是当前菜单的参数。 在function.php中没有功能

                    <?php
                        $menuParameters = array(
                          'theme_location' => 'primary',
                          'container'       => false,
                          'echo'            => false,
                          'items_wrap'      => '%3$s',
                          'depth'           => 0,
                        );

                        echo strip_tags(wp_nav_menu( $menuParameters ), '<a>' );
                    ?>

你有什么解决办法吗? 我指定“管理”>“导航”中的类不起作用

在您的后端中,转到Appearance -> Menus 在右上角有一个名为“ Screen Options的选项卡,在“显示高级菜单属性”中勾选CSS classes ,它将使您可以将adittional类添加到菜单项中。

您需要自定义导航菜单浏览器才能实现此目的。

首先,您需要删除walker上的<ul>, </ul>, <li>, and </li>标签,然后将css类移至<a>标签。

下面是我尝试过的助行器,将其粘贴在functions.php上:

class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {  
    public function start_lvl( &$output, $depth = 0, $args = array() ) {
        if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
            $t = '';
            $n = '';
        } else {
            $t = "\t";
            $n = "\n";
        }
        $indent = str_repeat( $t, $depth );

        $output .= "{$n}{$indent}{$n}";
    }

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

    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 ) );  

        $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['class']  = ! empty( $class_names )      ? $class_names      : '';

        $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 ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
            $t = '';
            $n = '';
        } else {
            $t = "\t";
            $n = "\n";
        }
        $output .= "{$n}";
    }
}

并设置您的wp_nav_menu参数:

<?php
$menuParameters = array(
  'theme_location' => 'primary',
  'container'       => false,
  'echo'            => false,
  'items_wrap'      => '%3$s',
  'depth'           => 0,
  'walker'          => new Custom_Walker_Nav_Menu()
);

// no need to strip tags since the custom walker already trimmed it
echo wp_nav_menu( $menuParameters );
?>

不要忘记在Appearance -> Menus设置CSS类

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM