将$Item->Description传递给Start_lvl

时间:2015-03-05 作者:Lee

使用时,是否有方法在第一个子菜单上显示父菜单描述start_lvl ?

function start_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\\t", $depth);
    $output .= "\\n$indent<ul class=\'sub-menu\'><li class=\'menu-image-container\'><div class=\'menu-image\'></div><div class=\'menu-description\'></div></li>\\n";
}
我想将父菜单项的描述放在menu-description 标签。

我知道你可以用start_el 获取$item->description 但是有没有办法把它传递给start_lvl ?

1 个回复
最合适的回答,由SO网友:Lee 整理而成
// Get menu description as global variable
function add_menu_description( $item_output, $item, $depth, $args ) {
    global $description;
    $description = $item->post_content;
    return $item_output;
}
add_filter( \'walker_nav_menu_start_el\', \'add_menu_description\', 10, 4);

// Submenu walker to add image
class submenu_walker extends Walker_Nav_Menu {
    function start_lvl( &$output, $depth = 0, $args = array() ) {
        if(0 == $depth) {
            global $description;
            $output .= \'<ul class="sub-menu">\'.
                \'<li class="row marginbottom1">\'.
                    \'<div class="col-sm-8 col-lg-9">\'.
                    \'<div class="menu-title">\'.
                            //$item->title.
                        \'</div>\'.
                        \'<div class="menu-description">\'.
                            $description.
                        \'</div>\'.
                    \'</div>\'.
                    \'<div class="col-sm-4 col-lg-3"><div class="menu-image img-rounded"></div></div>\'.
                \'</li>\';
        } else {
            $output .= \'<ul class="sub-menu">\';
        }
    }
}
结束