Wordpress wp_nav_menu walker:内容之前的动态

时间:2016-01-22 21:08:15

标签: php wordpress twitter-bootstrap menu

我在wordpress博客中构建导航。项目显示如下:

enter image description here

使用引导类glyhicon-glyphiconname应用图标。我动态添加类。 PHP代码:

function add_specific_menu_location_atts( $atts, $item, $args ) {
     // check if the item is in the primary menu
     if( $args->theme_location == 'directentries' ) {
       foreach($item as $key => $value) {
           if($key == 'title') {
             $catIcon = setCatIcon($value);
           }
       }
       // add the desired attributes:
       $atts['class'] = 'btn btn-primary btn-lg glyphicon glyphicon-'.$catIcon;
     }
     return $atts;
 }
 add_filter( 'nav_menu_link_attributes', 'add_specific_menu_location_atts', 10, 3 );

$args = array(
        'theme_location' => 'directentries',
        'depth'      => 1,
        'container'  => false,
        'menu_class'     => 'nav',
        'link_before'  => '<br>', 
        //'link_before'  => 'span class"glyphicon"',
        'walker'     => ''
    );
    wp_nav_menu($args);

这里的问题是,链接文本的字体(当然)是glyhicon,否则图标不适用于链接。所以:正确的方法是在初始化菜单时使用 link-before 参数应用 span 。 但我需要将动态类名应用于 span 。 我想我可以使用$ args参数访问我的过滤器类中的 link_before 参数。

目前的标记是这样的: enter image description here 我需要它来做这个: enter image description here 有没有人知道如何应用跨度并改变类?

2 个答案:

答案 0 :(得分:1)

<强> 1。首先在您的functions.php中添加以下代码。

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

        $class_names = '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;

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

        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

        $output .= $indent . '<li' . $id . $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        ) .'"' : '';

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

        if ( 'primary' == $args->theme_location ) {
            $submenus = 0 == $depth || 1 == $depth ? get_posts( array( 'post_type' => 'nav_menu_item', 'numberposts' => 1, 'meta_query' => array( array( 'key' => '_menu_item_menu_item_parent', 'value' => $item->ID, 'fields' => 'ids' ) ) ) ) : false;
            $item_output .= ! empty( $submenus ) ? '<span class="glyphicon"></span>' : '';
        }
        $item_output .= '</a>';
        $item_output .= $args->after;

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

<强> 2。将以下代码添加到您安装wp_nav_menu的header.php中。

下面解释的是代码,因此在这种情况下安装新的自定义菜单将是Nav_Walker_Nav_Menu

<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary', 'walker' => new Nav_Walker_Nav_Menu() ) ); ?>

答案 1 :(得分:0)

虽然@Trix建议自定义Nav Walker类可能是一个好的(更好的?)解决方案,但我想回答如何使用我的过滤器应用跨度的问题。这段代码做到了:

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("NoJs");

WebDriver driver = new FirefoxDriver(profile);
相关问题