update
This commit is contained in:
@@ -1,61 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* Bulma-Navwalker
|
||||
* Bulma Navwalker – secure, PHP 8.1 compatible rewrite
|
||||
*
|
||||
* @package Bulma-Navwalker
|
||||
* @package CyyWordpress
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class Name: Navwalker
|
||||
* Plugin Name: Bulma Navwalker
|
||||
* Plugin URI: https://github.com/Poruno/Bulma-Navwalker
|
||||
* Description: An extended Wordpress Navwalker object that displays Bulma framework's Navbar https://bulma.io/ in Wordpress.
|
||||
* Author: Carlo Operio - https://www.linkedin.com/in/carlooperio/, Bulma-Framework
|
||||
* Author URI: https://github.com/wp-bootstrap
|
||||
* License: GPL-3.0+
|
||||
* License URI: https://github.com/Poruno/Bulma-Navwalker/blob/master/LICENSE
|
||||
*/
|
||||
if ( ! class_exists( 'Bulmapress_Navwalker' ) ) :
|
||||
|
||||
class bulmapress_navwalker extends Walker_Nav_Menu {
|
||||
/**
|
||||
* Custom Bulma nav-walker for WordPress menus.
|
||||
*/
|
||||
class Bulmapress_Navwalker extends Walker_Nav_Menu {
|
||||
|
||||
public function start_lvl( &$output, $depth = 0, $args = array() ) {
|
||||
|
||||
$output .= "<div class='navbar-dropdown'>";
|
||||
}
|
||||
|
||||
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
|
||||
|
||||
$liClasses = 'navbar-item '.$item->title;
|
||||
|
||||
$hasChildren = $args->walker->has_children;
|
||||
$liClasses .= $hasChildren? " has-dropdown is-hoverable": "";
|
||||
|
||||
if($hasChildren){
|
||||
$output .= "<div class='".$liClasses."'>";
|
||||
$output .= "\n<a class='navbar-link' href='".$item->url."'>".$item->title."</a>";
|
||||
}
|
||||
else {
|
||||
$output .= "<a class='".$liClasses."' href='".$item->url."'>".$item->title;
|
||||
/**
|
||||
* Opens a dropdown container.
|
||||
*
|
||||
* @param string $output Used to append additional content (passed by reference).
|
||||
* @param int $depth Depth of menu item.
|
||||
* @param stdClass|null $args An object of wp_nav_menu() arguments.
|
||||
*/
|
||||
public function start_lvl( string &$output, int $depth = 0, ?stdClass $args = null ): void {
|
||||
$output .= '<div class="navbar-dropdown">';
|
||||
}
|
||||
|
||||
// Adds has_children class to the item so end_el can determine if the current element has children
|
||||
if ( $hasChildren ) {
|
||||
$item->classes[] = 'has_children';
|
||||
/**
|
||||
* Opens a menu item element and its link.
|
||||
*
|
||||
* @param string $output Used to append additional content (passed by reference).
|
||||
* @param WP_Post $item Menu item data object.
|
||||
* @param int $depth Depth of menu item.
|
||||
* @param stdClass|null $args An object of wp_nav_menu() arguments.
|
||||
* @param int $id Current item ID.
|
||||
*/
|
||||
public function start_el( string &$output, \WP_Post $item, int $depth = 0, ?stdClass $args = null, int $id = 0 ): void {
|
||||
$has_children = isset( $args->walker ) && $args->walker->has_children;
|
||||
|
||||
if ( $has_children ) {
|
||||
// Mark the item so end_el knows it needs a </div> instead of </a>.
|
||||
$item->classes[] = 'has_children';
|
||||
|
||||
$output .= '<div class="navbar-item has-dropdown is-hoverable">';
|
||||
$output .= '<a class="navbar-link" href="' . esc_url( $item->url ) . '">'
|
||||
. esc_html( $item->title )
|
||||
. '</a>';
|
||||
} else {
|
||||
$output .= '<a class="navbar-item" href="' . esc_url( $item->url ) . '">'
|
||||
. esc_html( $item->title );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes a menu item element.
|
||||
*
|
||||
* @param string $output Used to append additional content (passed by reference).
|
||||
* @param WP_Post $item Menu item data object.
|
||||
* @param int $depth Depth of menu item.
|
||||
* @param stdClass|null $args An object of wp_nav_menu() arguments.
|
||||
*/
|
||||
public function end_el( string &$output, \WP_Post $item, int $depth = 0, ?stdClass $args = null ): void {
|
||||
if ( in_array( 'has_children', (array) $item->classes, true ) ) {
|
||||
// Dropdown wrapper opened with <div>; close the div (inner <a> was already closed).
|
||||
$output .= '</div>';
|
||||
} else {
|
||||
// Regular item opened with <a>; close it.
|
||||
$output .= '</a>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes a dropdown container.
|
||||
*
|
||||
* @param string $output Used to append additional content (passed by reference).
|
||||
* @param int $depth Depth of menu item.
|
||||
* @param stdClass|null $args An object of wp_nav_menu() arguments.
|
||||
*/
|
||||
public function end_lvl( string &$output, int $depth = 0, ?stdClass $args = null ): void {
|
||||
$output .= '</div>'; // .navbar-dropdown
|
||||
}
|
||||
|
||||
/**
|
||||
* Fallback: output a link to the home page when no menu is assigned.
|
||||
*
|
||||
* @param array $args Arguments from wp_nav_menu().
|
||||
*/
|
||||
public static function fallback( array $args ): void {
|
||||
if ( current_user_can( 'manage_options' ) ) {
|
||||
echo '<a class="navbar-item" href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '">'
|
||||
. esc_html__( '请添加菜单', 'cyywordpress' )
|
||||
. '</a>';
|
||||
} else {
|
||||
echo '<a class="navbar-item" href="' . esc_url( home_url( '/' ) ) . '">'
|
||||
. esc_html__( '首页', 'cyywordpress' )
|
||||
. '</a>';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function end_el(&$output, $item, $depth = 0, $args = array(), $id = 0 ){
|
||||
|
||||
if(in_array("has_children", $item->classes)) {
|
||||
// Back-compat alias so existing wp_nav_menu() calls still work.
|
||||
class_alias( 'Bulmapress_Navwalker', 'bulmapress_navwalker' );
|
||||
|
||||
$output .= "</div>";
|
||||
}
|
||||
$output .= "</a>";
|
||||
}
|
||||
|
||||
public function end_lvl (&$output, $depth = 0, $args = array()) {
|
||||
|
||||
$output .= "</div>";
|
||||
}
|
||||
}
|
||||
?>
|
||||
endif;
|
||||
|
||||
Reference in New Issue
Block a user