【问题标题】:Wordpress wp_nav_menu nav > aWordpress wp_nav_menu 导航 > 一个
【发布时间】:2014-11-03 08:36:45
【问题描述】:

我实际上正在开发一个网站并开发一个主题。

我的 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>' );
                    ?>

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

【问题讨论】:

  • 为什么在 admin 中设置的类不起作用?
  • 我不知道 .. 也许这些类设置与
  • ..

标签: php wordpress class add nav


【解决方案1】:

在您的后端转到Appearance -&gt; Menus。在右上角有一个名为Screen Options 的选项卡,在“显示高级菜单属性”中勾选CSS classes,这将使您能够向菜单项添加附加类。

【讨论】:

  • 我指定管理中的类 > 导航不起作用
【解决方案2】:

您需要custom nav menu walker 来实现此目的。

首先,你需要去掉walker上的&lt;ul&gt;, &lt;/ul&gt;, &lt;li&gt;, and &lt;/li&gt;标签, 然后将css类移动到&lt;a&gt;标签。

下面是我试过的walker,粘贴到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 -&gt; Menus中设置css类

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签