我在一个网站上工作,该网站在现有的导航菜单中添加了自定义沃克。该代码对于标准wp菜单非常有用,但是我似乎无法在ubermenu(www.wpmegamenu.com)中添加相同的walker。
这是加载Wordpress菜单的主题标题中的代码:
wp_nav_menu( array( 'theme_location' => 'gold-bullion-menu', 'container' => '', 'fallback_cb' => '', 'menu_class' => 'nav', 'menu_id' => 'gold-bullion-menu', 'walker'=>new gold_bullion_menu_walker($json_data), 'echo' => false ) );
和沃克代码最后添加一个新的div,并通过JSON引入了一些外部信息:
class gold_bullion_menu_walker extends Walker_Nav_Menu{
/**
* @see Walker::start_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
var $data;
function __construct($data) {
$this->data = $data;
}
static $count=0;
function start_lvl( &$output, $depth = 0, $args = array() ) {
if(self::$count == 0)
$output .= "\n$indent<div class=\"spot-price spot-large gold\">NZD ". $this->data["spot-prices"]["AU"]["NZD"]["disp"] ."</div>\n";
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu\">\n"; //this is default output;
if( $depth==0 && self::$count == 0) //'0'==>1st-sub-level; '1'=2nd-sub-level; ....
$output .= "\n$indent<div class=\"spot-price spot-small gold\"><span class=\"label\">GOLD PRICE</span><span class=\"price\">NZD ". $this->data["spot-prices"]["AU"]["NZD"]["disp"] ."</span></div>\n";
self::$count++;
}
/**
* @see Walker::end_lvl()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
function end_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n"; //this is default output;
//if( $depth==0 ) //'0'==>1st-sub-level; '1'=2nd-sub-level; ....
//$output .= "$indent</ul></div></div>\n";
}
}
此代码可以正常工作-它在导航菜单的末尾添加了一个div,其中包含我需要的其他信息。 (还有很多其他JSON文件,但这里不需要它们)。这段代码可以完美地工作。
但是,如果我将wp_nav_menu更改为ubermenu,则walker不再起作用:
ubermenu('main', array( 'theme_location' => 'gold-bullion-menu', 'container' => '', 'fallback_cb' => '', 'menu_class' => 'nav', 'menu_id' => 'gold-bullion-menu', 'walker'=>new gold_bullion_menu_walker($json_data), 'echo' => false ) );
我从未写过原始代码,而且我也不完全了解沃克-我猜我做错了什么,但不确定是什么。