classes) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
// 添加深度类
$classes[] = 'menu-item-depth-' . $depth;
// 检查是否有子菜单
$has_children = in_array('menu-item-has-children', $classes);
if ($has_children) {
$classes[] = 'has-dropdown';
}
// 检查当前页面
if (in_array('current-menu-item', $classes) || in_array('current-menu-parent', $classes)) {
$classes[] = 'active';
}
$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 . '
';
$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 = isset($args->before) ? $args->before : '';
$item_output .= '';
$item_output .= (isset($args->link_before) ? $args->link_before : '') . apply_filters('the_title', $item->title, $item->ID) . (isset($args->link_after) ? $args->link_after : '');
// 如果有子菜单,添加下拉箭头
if ($has_children) {
$item_output .= ' ';
}
$item_output .= '';
$item_output .= isset($args->after) ? $args->after : '';
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
// 开始输出子菜单
function start_lvl(&$output, $depth = 0, $args = null) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent\n";
}
// 结束输出菜单项
function end_el(&$output, $item, $depth = 0, $args = null) {
$output .= "\n";
}
}
}
// 设置默认参数
$defaults = array(
'menu_id' => 'primary',
'menu_class' => '',
'theme' => 'default',
'show_border' => true,
'align' => 'left'
);
$args = wp_parse_args($args ?? array(), $defaults);
// 获取区块属性
$block_id = isset($block['id']) ? $block['id'] : 'navigation-menu-' . uniqid();
$class_name = 'navigation-menu-block';
// 添加主题类
$class_name .= ' theme-' . esc_attr($args['theme']);
// 添加对齐类
$class_name .= ' align-' . esc_attr($args['align']);
// 添加边框类
if ($args['show_border']) {
$class_name .= ' has-border';
}
// 添加自定义类
if (!empty($block['className'])) {
$class_name .= ' ' . $block['className'];
}
if (!empty($args['menu_class'])) {
$class_name .= ' ' . $args['menu_class'];
}
if (!empty($block['align'])) {
$class_name .= ' align' . $block['align'];
}
// 获取菜单
$menu = null;
if (is_numeric($args['menu_id'])) {
// 使用菜单ID
$menu = wp_get_nav_menu_object($args['menu_id']);
} else {
// 使用菜单名称或位置
$locations = get_nav_menu_locations();
if (isset($locations[$args['menu_id']])) {
$menu = wp_get_nav_menu_object($locations[$args['menu_id']]);
} else {
// 尝试按名称查找菜单
$menu = wp_get_nav_menu_object($args['menu_id']);
}
}
// 如果没有找到指定菜单,尝试获取主菜单
if (!$menu) {
$locations = get_nav_menu_locations();
if (isset($locations['primary'])) {
$menu = wp_get_nav_menu_object($locations['primary']);
}
}
?>