Wordpress wp_nav_menu nav>一个

时间:2014-09-09 20:11:00

标签: php wordpress class add nav

我实际上在一个网站上工作,我开发了一个主题。

我遇到了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>' );
                    ?>

你有任何解决方案吗? 我指定管理中的类&gt;导航没有工作

2 个答案:

答案 0 :(得分:0)

在你的后端转到Appearance -> Menus。在右上角有一个名为Screen Options的标签,位于“显示高级菜单属性”标记CSS classes,可让您在菜单项中添加adittional类。

答案 1 :(得分:0)

您需要custom nav menu 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类