找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3242|回复: 0

WordPress使用wp_nav_menu函数生成需要的class结构

[复制链接]

37

主题

0

回帖

257

积分

管理员

积分
257
发表于 2024-8-18 19:11:56 | 显示全部楼层 |阅读模式
WordPress 制作导航菜单,会使用 wp_nav_menu 函数,它可以自动调用出后台创建的导航菜单。但是使用默认的 wp_nav_menu 函数生成的菜单结构比较单调,有时很难达到我们自己的需要。下面介绍一下 WordPress 如何使用 wp_nav_menu 函数生成需要的 class 结构。
第一步:将下的函数代码粘贴到自己模板的 Functions.php 中;
  • *自定义二级菜单导航开始*/
  • class wp_bootstrap_navwalker extends Walker_Nav_Menu {public function start_lvl( &$output, $depth = 0, $args = array() ) {
  • $indent = str_repeat( "\t", $depth );
  • $output .= $indent.'<ul class="dropdown-menu">';//00001-下拉UL样式
  • }
  • public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  • $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  • //判断标题文字是不是指定文字,然后显示样式
  • if ( strcasecmp( $item->attr_title, 'divider' ) == 0 && $depth === 1 ) {
  • $output .= $indent . '<li role="presentation" class="divider">';
  • } else if ( strcasecmp( $item->title, 'divider') == 0 && $depth === 1 ) {
  • $output .= $indent . '<li role="presentation" class="divider">';
  • } else if ( strcasecmp( $item->attr_title, 'dropdown-header') == 0 && $depth === 1 ) {
  • $output .= $indent . '<li role="presentation" class="dropdown-header">' . esc_attr( $item->title );
  • } else if ( strcasecmp($item->attr_title, 'disabled' ) == 0 ) {
  • $output .= $indent . '<li role="presentation" class="disabled"><a href="#">' . esc_attr( $item->title ) . '</a>';
  • //判断结束
  • } else {
  • $class_names = $value = '';
  • $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 ) );
  • if ( $args->has_children )
  • $class_names .= ' dropdown nav1';//00002-有下拉菜单的父LI的样式
  • if ( !$args->has_children && $depth === 0 )
  • $class_names .= ' nav1';//00003-没有下拉菜单的父LI的样式
  • if ( in_array( 'current-menu-item', $classes ) )
  • $class_names .= ' active';//00004-当前样式
  • $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  • $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );//00005-菜单所有li的添加ID
  • $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  • $output .= $indent . '<li' . $id . $value . $class_names .'>';
  • $atts = array();
  • $atts['title'] = ! empty( $item->title ) ? $item->title : '';
  • $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  • $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  • // If item has_children add atts to a.
  • if ( $args->has_children && $depth === 0 ) {
  • $atts['href'] = $item->url;
  • $atts['data-toggle'] = 'dropdown';
  • $atts['class'] = 'alink';//00006-有下拉的第一个父A的CLASS
  • $atts['aria-haspopup'] = 'true';//控制父A链接是否可点击
  • } else {
  • $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  • }
  • $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
  • $attributes = '';
  • foreach ( $atts as $attr => $value ) {
  • if ( ! empty( $value ) ) {
  • $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  • $attributes .= ' ' . $attr . '="' . $value . '"';
  • }
  • }
  • $item_output = $args->before;
  • if ( $args->has_children && 0 === $depth )
  • $item_output .= '<a data-toggle="dropdown"'. $attributes .'>';//00007-有下拉的第一个父A里面加内容
  • elseif ( ! empty( $item->attr_title ) )
  • $item_output .= '<a'. $attributes .'><span class="glyphicon ' . esc_attr( $item->attr_title ) . '"></span>&nbsp;';
  • else
  • $item_output .= '<a'. $attributes .'>';
  • $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  • $item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';////00008-有下拉的第一个父A后面加span,也可以加到外面
  • $item_output .= $args->after;
  • $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  • }
  • }
  • public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
  • if ( ! $element )
  • return;
  • $id_field = $this->db_fields['id'];
  • // Display this element.
  • if ( is_object( $args[0] ) )
  • $args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] );
  • parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  • }
  • }




第二步:使用以下的代码来调用菜单

  • <?php
  • wp_nav_menu( array(
  • 'theme_location' => 'topmenu',
  • 'depth' => 2,
  • 'container' => false,
  • 'menu_class' => 'nav navbar-nav navlist',
  • 'menu_id' => 'topmeau',
  • 'fallback_cb' => 'wp_page_menu',
  • //添加或更改walker参数
  • 'walker' => new wp_bootstrap_navwalker())
  • );
  • ?>



如果想修改 class 类,可以在第一步的代码中修改,达到自己需要的 class 结构。

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|极客云码 ( 豫ICP备14014030号-10 )

GMT+8, 2025-5-21 18:38 , Processed in 0.083244 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表