【问题标题】:Add different span class to wp nav menu将不同的跨度类添加到 wp 导航菜单
【发布时间】:2014-10-12 01:41:06
【问题描述】:

这是我的 html 菜单:

<div id="main-menu-container">
<ul id="main-menu">
<li><a href="#"><span aria-hidden="true" class="icon-home"></span>Home</a></li>
<li><a href="#"><span aria-hidden="true" class="icon-briefcase"></span>Portfolio</a></li>
li><a href="#"><span aria-hidden="true" class="icon-cog"></span>Services</a></li>
</ul>
</div>

& WordPress 导航代码:

<?php 
wp_nav_menu( 
array(
 'theme_location' => 'primary',
'container_id' => 'main-menu-container',
'menu_id' => 'main-menu',
) 
); 
?>

如您所见,问题是,我为每个列表项添加了不同的跨度类。我不能使用link_beforelink_after,因为跨度类在链接之间,而不是之后和之前。

有没有办法把这种html菜单转成WordPress?

【问题讨论】:

标签: html css wordpress menu


【解决方案1】:

您需要使用Custom Walker。您可以在下面查看带有several sample code sources here 或更通用版本的教程。

添加到functions.php

class description_walker extends Walker_Nav_Menu
{
  function start_el(&$output, $item, $depth, $args)
  {
       global $wp_query;
       $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

       $class_names = $value = '';

       $classes = empty( $item->classes ) ? array() : (array) $item->classes;

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

       $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

       $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
       $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
       $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
       $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

       $prepend = '<strong>';
       $append = '</strong>';
       $description  = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';

       if($depth != 0)
       {
                 $description = $append = $prepend = "";
       }

        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
        $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
        $item_output .= $description.$args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;

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

您的 wp_nav_menu 代码

    wp_nav_menu( array(
 'container' =>false,
 'menu_class' => 'nav',
 'echo' => true,
 'before' => '',
 'after' => '',
 'link_before' => '',
 'link_after' => '',
 'depth' => 0,
 'walker' => new description_walker())
     ) ; 

Kriesi提取的代码

【讨论】:

  • 我会尝试并回复您。我想我必须编辑 item_output 变量以使其按我想要的方式工作。
猜你喜欢
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
相关资源
最近更新 更多