php - Custom Walker - split child menu items into columns
one text
I have this pretty basic custom walker for my nav. Is there any way to split the child menu items into columns, for example, if we have 6 child nav items in the first nav item, how can I put 3 in one column and 3 in another column?
I know I can do that with CSS, but I would extend it further to group items based on their count and that won't work with CSS.
class SC_Menu_Walker extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth=0, $args=[], $id=0) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$this->curItem = $item;
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
$class_names = ' class="'. esc_attr( $class_names ) . '"';
$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
if ($item->url && $item->url != '#') {
$output .= '<a href="' . $item->url . '">';
} else {
$output .= '<span>';
}
$output .= $item->title;
}
function start_lvl(&$output, $depth = 0, $args = array()) {
$sc_page_item_label = get_post_meta( $this->curItem->object_id, '_sc_page_item_label', true );
$sc_page_item_description = get_post_meta( $this->curItem->object_id, '_sc_page_item_description', true );
$sc_page_item_url = get_post_meta( $this->curItem->object_id, '_sc_page_item_url', true );
$sc_page_item_page_url = get_page_link($sc_page_item_url);
$secondMenuClass="sub-menu__list o-sm-col-12 o-lg-col-7 u-font-size-medium";
$wrapperScrollStart="";
if ($depth == 1) {
$secondMenuClass="sub-menu__list sub-menu__list--scrollbar";
$wrapperScrollStart="<div class='sub-menu__inner'>";
$wrapperScrollEnd ='</div>';
}
$output .= '<div class="sub-menu sub">
<div class="o-custom-container">
<div class="sub-menu__wrapper_info o-sm-col-12 o-lg-col-5"><div class="o-lg-col-7"><h5>'.$sc_page_item_label.'</h5> <div class="u-font-size-medium">'.$sc_page_item_description.'</div></div>
<div>
<div class="c-learn-more-holder">
<div class="c-learn-more">
<a href="'.$sc_page_item_page_url.'" class="c-learn-more-link "></a>
<div class="c-learn-more-title">Learn more</div>
</div>
</div>
</div>
</div>'.$wrapperScrollStart.'<ul class="'.$secondMenuClass.'">';
}
//end of the sub menu wrap
function end_lvl(&$output, $depth = 0, $args = array()) {
$wrapperScrollEnd = '';
if ($depth == 1) {
$wrapperScrollEnd ='</div>';
}
$wrapperScrollEnd ='</div>';
$output .= '</ul>'.$wrapperScrollEnd.'</div>';
}
}
Source