【问题标题】:Navwalker won't accept css classes defined in menuNavwalker 不接受菜单中定义的 CSS 类
【发布时间】:2020-08-11 13:06:48
【问题描述】:

所以我正在使用带有 Bulma Navwalker 的 Bulma 框架 - 我遇到的问题是它不接受在我的 WordPress 菜单中定义的自定义 css 类。

所以我在我的一个父菜单项中定义了一个 is-mega 类,它没有显示在“navbar-item has-dropdown is-hoverable”下。

有人看到我可能会失踪吗?

这是导航行者:

<?php
/**
 * Bulma-Navwalker
 *
 * @package Bulma-Navwalker
 */

/**
 * 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
 */

class 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 ';

        $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;
        }

        // Adds has_children class to the item so end_el can determine if the current element has children
        if ( $hasChildren ) {
            $item->classes[] = 'has_children';
        }
    }

    public function end_el(&$output, $item, $depth = 0, $args = array(), $id = 0 ){

        if(in_array("has_children", $item->classes)) {

            $output .= "</div>";
        }
        $output .= "</a>";
    }

    public function end_lvl (&$output, $depth = 0, $args = array()) {

        $output .= "</div>";
    }
}
?>

【问题讨论】:

  • 我会询问 Bulma 开发人员,因为看起来 Navwalker 不会将菜单项类添加到 $liClasses 变量中。

标签: wordpress flexbox bulma


【解决方案1】:

您可以通过稍微调整助行器来实现这一点。 Bulmapress 从 walker 中删除了标准类,以便它可以使用自己的类。通过添加它,它将允许您从 Appearance->Menu 添加自定义类。

<?php

public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

        // Add your custom item classes here
        
        $custom_classes_from_menu = implode(' ', $item->classes); // turn the WP classes object array to string values
    
        $liClasses = 'navbar-item ' . $custom_classes_from_menu; // add the values to your classes list

        $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;
        }

        // Adds has_children class to the item so end_el can determine if the current element has children
        if ( $hasChildren ) {
            $item->classes[] = 'has_children';
        }
    }

【讨论】:

  • 非常感谢 DubVader!这非常有效 - 您是否有机会使用 Bulma 制作 wordpress 大型菜单?
  • 没问题。有一个 Bulma 扩展程序可以满足您的需求。在不知道具体细节的情况下,很难准确理解如何实现,但这应该会有所帮助:github.com/hunzaboy/bulma-megamenu
猜你喜欢
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 2018-12-26
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
相关资源
最近更新 更多