✏️ 正在编辑: wp-walker.php
路径:
/home/h359620/public_html/wp-content/themes/datis/inc/includes/wp-walker.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php if (!defined('ABSPATH')) { exit; } if (!class_exists('Datis_Walker')) { class Datis_Walker extends \Walker_Nav_Menu { private static $instance; private function __construct() { $this->init_hooks(); } private function init_hooks() { add_action('wp_nav_menu_item_custom_fields', array($this, 'add_menu_item_icon_fields'), 10, 4); add_action('wp_update_nav_menu_item', array($this, 'save_menu_item_icon_fields'), 10, 3); add_action('admin_enqueue_scripts', array($this, 'enqueue_datis_menu_script')); } public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) { $classes = empty($item->classes) ? array() : (array) $item->classes; $classes[] = 'menu-item-' . $item->ID; $template_id = get_post_meta($item->ID, '_menu_item_elementor_template', true); $is_mega_menu = !empty($template_id); if ($is_mega_menu) { $classes[] = 'datis-mega-menu'; } $post_id = $item->object_id; $data_menu_id = 'data-menu-id="' . esc_attr($post_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 .= '<li' . $id . $class_names . ' ' . $data_menu_id . '>'; // ------------------------- // ✅ Attributes (NOW href always exists if URL exists) // ------------------------- $atts = array(); $atts['title'] = !empty($item->attr_title) ? $item->attr_title : ''; $atts['target'] = !empty($item->target) ? $item->target : ''; $atts['rel'] = !empty($item->xfn) ? $item->xfn : ''; // ✅ allow href also for mega menus $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 . '"'; } } $title = apply_filters('the_title', $item->title, $item->ID); $svg_icon = get_post_meta($item->ID, '_menu_item_svg_icon', true); $fa_icon = get_post_meta($item->ID, '_menu_item_fa_icon', true); $item_output = $args->before; // ✅ Always render <a> even for mega menu $item_output .= '<a' . $attributes . ' class="menu-link">'; // Icons if ($svg_icon) { $svg_path = ABSPATH . str_replace(site_url(), '', $svg_icon); if (file_exists($svg_path)) { $svg_content = file_get_contents($svg_path); $item_output .= '<span class="menu-icon svg-icon">' . $svg_content . '</span>'; } else { $item_output .= '<img src="' . esc_url($svg_icon) . '" alt="' . esc_attr($title) . '" class="menu-icon fallback-svg-icon" />'; } } elseif ($fa_icon) { $item_output .= '<i class="' . esc_attr($fa_icon) . ' menu-icon fa-icon"></i>'; } // Title + child indicator if (in_array('menu-item-has-children', $classes)) { $item_output .= '<div class="menu-text-wrapper">'; $item_output .= $args->link_before . $title . $args->link_after; $item_output .= '<span class="submenu-icon" aria-hidden="true">'; $item_output .= '<svg width="22" height="22" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">'; $item_output .= '<path d="M14.25 6.75L9 11.25L7.6875 10.125M3.75 6.75L5.5 8.25" stroke="currentColor" stroke-opacity="1" stroke-width="1.125" stroke-linecap="round" stroke-linejoin="round"/>'; $item_output .= '</svg>'; $item_output .= '</span>'; $item_output .= '</div>'; } else { $item_output .= $args->link_before . $title . $args->link_after; } $item_output .= '</a>'; // ✅ Mega menu toggle icon (button) – does NOT steal the link click if ($is_mega_menu) { $item_output .= '<button type="button" class="mega-menu-icon" aria-expanded="false" aria-label="' . esc_attr__('Open menu', 'datis') . '">'; $item_output .= '<svg width="22" height="22" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">'; $item_output .= '<path d="M14.25 6.75L9 11.25L7.6875 10.125M3.75 6.75L5.5 8.25" stroke="currentColor" stroke-opacity="1" stroke-width="1.125" stroke-linecap="round" stroke-linejoin="round"></path>'; $item_output .= '</svg>'; $item_output .= '</button>'; } $item_output .= $args->after; $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args); // Mega menu content if ($is_mega_menu) { $output .= '<div class="datis-megamenu-wrapper">'; $output .= \Elementor\Plugin::$instance->frontend->get_builder_content_for_display($template_id); $output .= '</div>'; } } function add_menu_item_icon_fields($item_id, $item, $depth, $args) { $svg_icon = get_post_meta($item_id, '_menu_item_svg_icon', true); $fa_icon = get_post_meta($item_id, '_menu_item_fa_icon', true); $template_id = get_post_meta($item_id, '_menu_item_elementor_template', true); $templates = get_posts([ 'post_type' => 'elementor_library', 'numberposts' => -1, 'post_status' => 'publish' ]); ?> <p class="description description-wide"> <label for="edit-menu-item-svg-icon-<?php echo esc_attr($item_id); ?>"> <?php esc_html_e('SVG Icon URL', 'datis'); ?><br> <input type="text" id="edit-menu-item-svg-icon-<?php echo esc_attr($item_id); ?>" class="widefat code edit-menu-item-svg-icon" name="menu-item-svg-icon[<?php echo esc_attr($item_id); ?>]" value="<?php echo esc_attr($svg_icon); ?>" /> <button type="button" class="button button-secondary select-svg-icon"><?php esc_html_e('Select SVG', 'datis'); ?></button> </label> </p> <p class="description description-wide"> <label for="edit-menu-item-fa-icon-<?php echo esc_attr($item_id); ?>"> <?php esc_html_e('Font Awesome Icon Class', 'datis'); ?><br> <input type="text" id="edit-menu-item-fa-icon-<?php echo esc_attr($item_id); ?>" class="widefat code edit-menu-item-fa-icon" name="menu-item-fa-icon[<?php echo esc_attr($item_id); ?>]" value="<?php echo esc_attr($fa_icon); ?>" /> <button type="button" class="button button-secondary select-fa-icon"><?php esc_html_e('Select Icon', 'datis'); ?></button> </label> </p> <p class="description description-wide"> <label for="edit-menu-item-elementor-template-<?php echo esc_attr($item_id); ?>"> <?php esc_html_e('Elementor Template for Megamenu', 'datis'); ?><br> <select name="menu-item-elementor-template[<?php echo esc_attr($item_id); ?>]" class="widefat"> <option value=""><?php esc_html_e('— Select Template —', 'datis'); ?></option> <?php foreach ($templates as $template) : ?> <option value="<?php echo esc_attr($template->ID); ?>" <?php selected($template_id, $template->ID); ?>> <?php echo esc_html($template->post_title); ?> </option> <?php endforeach; ?> </select> </label> </p> <?php } function save_menu_item_icon_fields($menu_id, $menu_item_db_id, $args) { if (isset($_POST['menu-item-svg-icon'][$menu_item_db_id])) { $sanitized_svg = sanitize_text_field($_POST['menu-item-svg-icon'][$menu_item_db_id]); update_post_meta($menu_item_db_id, '_menu_item_svg_icon', $sanitized_svg); } else { delete_post_meta($menu_item_db_id, '_menu_item_svg_icon'); } if (isset($_POST['menu-item-fa-icon'][$menu_item_db_id])) { $sanitized_fa_icon = sanitize_text_field($_POST['menu-item-fa-icon'][$menu_item_db_id]); update_post_meta($menu_item_db_id, '_menu_item_fa_icon', $sanitized_fa_icon); } else { delete_post_meta($menu_item_db_id, '_menu_item_fa_icon'); } if (isset($_POST['menu-item-elementor-template'][$menu_item_db_id])) { $template_id = absint($_POST['menu-item-elementor-template'][$menu_item_db_id]); update_post_meta($menu_item_db_id, '_menu_item_elementor_template', $template_id); } else { delete_post_meta($menu_item_db_id, '_menu_item_elementor_template'); } } function enqueue_datis_menu_script($hook) { if ($hook != 'nav-menus.php') { return; } wp_enqueue_media(); wp_enqueue_script('datis-menu-script', DATIS_URI . '/assets/js/menu-script.js', array(), null, true); } public static function get_instance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } } Datis_Walker::get_instance(); }
💾 保存文件
← 返回文件管理器