You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* Block Template: Navigation Menu
* 导航菜单区块模板
*
* @package Nenghui Energy Theme
* @since 1.0.0
*
* @param array $args {
* Optional. Array of arguments.
* @type string|int $menu_id 菜单ID或菜单名称默认为主菜单
* @type string $menu_class 菜单容器CSS类名默认为空
* @type string $theme 菜单主题样式,可选值:'default', 'dark', 'light'
* @type bool $show_border 是否显示下边框默认为true
* @type string $align 对齐方式,可选值:'left', 'center', 'right'
* }
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
/**
* 自定义导航菜单Walker类
*/
if (!class_exists('Navigation_Menu_Walker')) {
class Navigation_Menu_Walker extends Walker_Nav_Menu {
// 开始输出菜单项
function start_el(&$output, $item, $depth = 0, $args = null, $id = 0) {
$indent = ($depth) ? str_repeat("\t", $depth) : '';
$classes = empty($item->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 . '<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 = isset($args->before) ? $args->before : '';
$item_output .= '<a' . $attributes . '>';
$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 .= ' <span class="dropdown-arrow"></span>';
}
$item_output .= '</a>';
$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<ul class=\"sub-menu dropdown-menu depth-$depth\">\n";
}
// 结束输出子菜单
function end_lvl(&$output, $depth = 0, $args = null) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}
// 结束输出菜单项
function end_el(&$output, $item, $depth = 0, $args = null) {
$output .= "</li>\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']);
}
}
?>
<div id="<?php echo esc_attr($block_id); ?>" class="<?php echo esc_attr($class_name); ?>">
<div class="navigation-menu-container">
<?php if ($menu && !is_wp_error($menu)): ?>
<nav class="navigation-menu-nav" role="navigation" aria-label="<?php echo esc_attr($menu->name); ?>">
<?php
wp_nav_menu(array(
'menu' => $menu->term_id,
'menu_class' => 'navigation-menu-list',
'menu_id' => 'menu-' . $menu->slug,
'container' => false,
'depth' => 3,
'fallback_cb' => false,
'walker' => new Navigation_Menu_Walker(),
));
?>
</nav>
<?php else: ?>
<div class="navigation-menu-fallback">
<p class="no-menu-message">
<?php _e('未找到指定的菜单。请检查菜单ID或在WordPress后台创建菜单。', 'nenghui-energy-theme'); ?>
</p>
<?php if (current_user_can('manage_options')): ?>
<p class="admin-notice">
<a href="<?php echo admin_url('nav-menus.php'); ?>" target="_blank">
<?php _e('前往菜单管理', 'nenghui-energy-theme'); ?>
</a>
</p>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
</div>