当前位置: 首页 > 网站建设 > WordPress开发 > WordPress输出LayUI样式的菜单结构

WordPress输出LayUI样式的菜单结构

发布于:2019-10-27 WordPress开发 0条评论 5,383 views
本站提供Linux服务器运维,自动化脚本编写等服务,如有需要请联系博主微信:xiaozme

LayUI是由“贤心”开发的一款前端框架,使用简单、方便,和知名的BootStrap框架类似,目前xiaoz大多数开源项目正是使用的LayUI框架。但使用LayUI来开发WordPress主题的开发者比较少,所以也存在一些坑,就比如WordPress默认使用wp_nav_menu()函数来输出菜单结构,和LayUI的菜单结构不一致,导致样式非常错乱,在此记录下解决办法。

重写wp_nav_menu()函数

将下面的代码添加到主题目录下的functions.php文件中:

#-----------------------------------------------------------------#
# 修改wp_nav_menu的li标签
#-----------------------------------------------------------------#
class new_walker extends Walker_Nav_Menu{
    //修改一级ul标签样式
    function start_lvl( &$output, $depth = 0, $args = array() ){
        if( $depth == 0 ){
            $output .= '<ul class="layui-nav-child">';
        }else{
            $output .= '<ul class="layui-nav-child">';
        }
    }
    function end_lvl( &$output, $depth = 0, $args = array() ){
        if( $depth == 0 ){
            $output .= "</ul>";
        }else{
            $output .= '</ul>';
        }
    }
    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 ) );
        if( $depth == 0 ){
            $class_names .= ' layui-nav-item'; //增加Layui样式
        }
        if ( in_array( 'current-menu-item', $classes ) ){
            $class_names .= ' layui-this'; //增加当前样式
        }
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

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

        $output .= $indent . '<li' . $id . $class_names . '>';

        $atts           = array();
        $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
        $atts['target'] = ! empty( $item->target ) ? $item->target : '';
        if ( '_blank' === $item->target && empty( $item->xfn ) ) {
        $atts['rel'] = 'noopener noreferrer';
        } else {
        $atts['rel'] = $item->xfn;
        }
        $atts['href']         = ! empty( $item->url ) ? $item->url : '';
        $atts['aria-current'] = $item->current ? 'page' : '';

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

前端调用

使用下方的代码即可调用菜单,并且生成的LayUI样式风格。

<?php
  wp_nav_menu( array( 
    'theme_location' => '', //导航别名
    'menu_class' => 'layui-nav', //引用layui-nav样式
    'walker' => new new_walker(), //引用刚才的重构
  ));
?>

上方theme_location参数为空,指的是默认调用第一个主菜单,但是如果您注册的菜单不是主菜单,导航栏可能无法显示,则需要修改为自己注册的菜单别名。比如:

<?php
    wp_nav_menu( array( 
    'theme_location' => 'header-menu', //导航别名
    'menu_class' => 'layui-nav', //引用layui-nav样式
    'walker' => new new_walker(), //引用刚才的重构
    ));
?>

其中header-menu就是您自己注册的菜单别名

此文部分内容参考了:wordpress输出LayUI的菜单结构


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注