使用插件创建了一个名为Country的自定义菜单列表,其中包含代表国家(仅加拿大和美国)的标志Menu Image
我将此菜单与Walker相结合,创建了一个下拉菜单。但是,我只能访问分配给图像的标题,无法找出图像信息实际存储在$item
.
这是我使用的Walker类,它在下拉列表中显示所选项目的标题。
class Walker_Nav_Menu_Dropdown extends Walker_Nav_Menu {
function start_lvl(&$output, $depth){
$indent = str_repeat("\\t", $depth); // don\'t output children opening tag (`<ul>`)
}
function end_lvl(&$output, $depth){
$indent = str_repeat("\\t", $depth); // don\'t output children closing tag
}
/**
* Start the element output.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. May be used for padding.
* @param array $args Additional strings.
* @return void
*/
function start_el(&$output, $item, $depth, $args) {
//var_dump($item); exit;
foreach ($item as $key=>$value){
if(strpos(strtolower((string)$value),"anada")!== false){
echo $key .":".$value."&&";
}
}
$url = \'#\' !== $item->url ? $item->url : \'\';
$output .= \'<option value="\' . $url . \'">\' . $item->title;
// $item->image;
}
function end_el(&$output, $item, $depth){
$output .= "</option>\\n"; // replace closing </li> with the option tag
}
}
调用该类是在我的主题内进行的,如下所示:
<?php
// Nav Menu Dropdown Class
include_once( \'/lib/classes/nav-menu-dropdown.php\' );
wp_nav_menu( array(
// \'theme_location\' => \'mobile\',
\'menu\' => \'Country\',
\'walker\' => new Walker_Nav_Menu_Dropdown(),
\'items_wrap\' => \'<div class="mobile-menu"><form><select onchange="if (this.value) window.location.href=this.value">%3$s</select></form></div>\',
) );
?>
我有var\\u提供的信息,但我找不到任何帮助。我还使用以下标准方法输出菜单
wp_nav_menu( array( \'menu\' => \'Country\'));
上面显示了我想在下拉列表中看到的菜单图像,我知道图像存储在哪里,但我想从$item
当我创建walker下拉列表时
Update 1: 修改为function start_el
function start_el(&$output, $item, $depth, $args) {
$menuImage = new Menu_Image_Plugin();
$menuImage->menu_image_nav_menu_item_filter($output2, $item, $depth, $args);
var_dump($output2);
$url = \'#\' !== $item->url ? $item->url : \'\';
$output .= \'<option value="\' . $url . \'">\' . $output2;
// $item->image;
}
function end_el(&$output, $item, $depth){
$output .= "</option>\\n"; // replace closing </li> with the option tag
}
}
输出自
$menuImage->menu_image_nav_menu_item_filter()
是
NULL
.
Update 2
审查了的功能
menu_image_nav_menu_item_filter
在menu\\u image中。php
function start_el(&$output, $item, $depth, $args) {
$image_size = $item->image_size ? $item->image_size : apply_filters( \'menu_image_default_size\', \'menu-36x36\' );
$image = wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
var_dump($image); //read string out
echo $image; //displays the image
$url = \'#\' !== $item->url ? $item->url : \'\';
$output .= \'<option value="\' . $url . \'">\' . $image;
// image is not being included in $output
}
function end_el(&$output, $item, $depth){
$output .= "</option>\\n"; // replace closing </li> with the option tag
}
}
图像仍不包括在输出中?