Site icon Hip-Hop Website Design and Development

How to Display sub items if the Menu first level is Category selected in Nav walker

I have a problem in display nav walker. The code below is for the mega menu.

In WordPress Menu When the first item selected is a category, it is not showing sub items.

In normal mode, in depth=0 if a menu item has a sub-menu, the mega Menu will be displayed. Now I want if the first menu item was (active) and the menu item was category, however the Mega menu will still be displayed.

In other words, if I not add any sub menu item in WordPress menu setting, however the Mega menu will still be displayed in frontend.
In your current source if depth=0 has sub menu item mega Menu will be displayed.

I want in depth=0 if first item doesn’t have any sub menu and selected item is a category, however the Mega menu will still be displayed.

Unfortunately, whatever I did, I could not do that. Please help me how can I solve this problem thanks a lot

The nav walker source codes:

class walkernav extends Walker_Nav_Menu{
private $hany_megamenu_icon          = '';
private $hany_megamenu_image         = '';
private $hany_megamenu_position      = '';
private $hany_megamenu_position_y    = '';
private $hany_megamenu_repeat        = '';
private $hany_megamenu_min_height    = '';
private $hany_megamenu_padding_left  = '';
private $hany_megamenu_padding_right = '';
private $hany_megamenu_fivecolumn    = '';
private $hany_megamenu_category      = '';
private $hany_has_children           = '';
public  $megaMenuID;
private $curItem;
private $curColumn;
private $curCat;
public $count;
public function __construct()
{
$this->megaMenuID = 0;
$this->count = 0;
}
public function start_lvl(&$output, $depth = 0, $args = array())
{
$item    = $this->curItem;
$indent  = str_repeat("t", $depth);
$submenu = ($depth > 0) ? ' sub-menu' : '';
//** display mega menu image background and styles --- start
$megamenu_img_fields = megamenu_img_fields();
foreach( $megamenu_img_fields as $custom_meta_field ){
$save_option_name   = str_replace( 'menu-item-', '', $custom_meta_field);
$save_option_name   = str_replace( '-', '_', $save_option_name);
if($save_option_name == 'hany_megamenu_image'){
$hany_megamenu_image = get_post_meta( $item->ID, $save_option_name, true );
}
elseif($save_option_name == 'hany_megamenu_position'){
$hany_megamenu_position = get_post_meta( $item->ID, $save_option_name, true );
}
elseif($save_option_name == 'hany_megamenu_position_y'){
$hany_megamenu_position_y = get_post_meta( $item->ID, $save_option_name, true );
}
elseif($save_option_name == 'hany_megamenu_repeat'){
$hany_megamenu_repeat = get_post_meta( $item->ID, $save_option_name, true );
}
elseif($save_option_name == 'hany_megamenu_padding_right'){
$hany_megamenu_padding_right = get_post_meta( $item->ID, $save_option_name, true );
}
elseif($save_option_name == 'hany_megamenu_padding_left'){
$hany_megamenu_padding_left = get_post_meta( $item->ID, $save_option_name, true );
}
elseif($save_option_name == 'hany_megamenu_min_height'){
$hany_megamenu_min_height = get_post_meta( $item->ID, $save_option_name, true );
}
elseif($save_option_name == 'hany_megamenu_fivecolumn'){
$hany_megamenu_fivecolumn = get_post_meta( $item->ID, $save_option_name, true );
}
elseif($save_option_name == 'hany_megamenu_category'){
$hany_megamenu_category = get_post_meta( $item->ID, $save_option_name, true );
}
$back_img       = '';
$pos_img        = '';
$pos_img_y      = '';
$rep_img        = '';
$pad_left_img   = '';
$pad_right_img  = '';
$min_height_img = '';
$num_col        = '';
$sub_cat        = '';
$all_post       = '';
if(! empty( $hany_megamenu_image )){
if(! empty( $hany_megamenu_position )){
$pos_img = "background-position:".esc_attr($hany_megamenu_position);
}
if(! empty($hany_megamenu_position_y)){
$pos_img_y = esc_attr($hany_megamenu_position_y).";";
}
if(! empty($hany_megamenu_repeat)){
$rep_img = "background-repeat:".esc_attr($hany_megamenu_repeat).";";
}
$back_img = " background-image: url(".esc_attr($hany_megamenu_image).");".$pos_img." ".$pos_img_y.$rep_img;
}
if ( ! empty( $hany_megamenu_padding_left ) ){
$padding_left = esc_attr($hany_megamenu_padding_left);
if ( strpos( $padding_left , 'px' ) === false && strpos( $padding_left , '%' ) === false ) $padding_left .= 'px';
$pad_left_img .= " padding-left : $padding_left !important; ";
}
if ( ! empty( $hany_megamenu_padding_right) ){
$padding_right = esc_attr($hany_megamenu_padding_right);
if ( strpos( $padding_right , 'px' ) === false && strpos( $padding_right , '%' ) === false ) $padding_right .= 'px';
$pad_right_img .= " padding-right : $padding_right !important; ";
}
if ( ! empty( $hany_megamenu_min_height ) ){
$min_height = esc_attr($hany_megamenu_min_height);
if ( strpos( $min_height , 'px' ) === false ) $min_height .= 'px';
$min_height_img .= " min-height : $min_height; ";
}
if(! empty( $hany_megamenu_fivecolumn )){
$this->curColumn = $hany_megamenu_fivecolumn;
$num_col = $this->curColumn;
}else{
$this->curColumn = 'column-4';
$num_col = $this->curColumn;
}
if(! empty( $hany_megamenu_category ) && $item->type == 'taxonomy' ){
$this->curCat = 'cat';
$query_args = apply_filters( 'child_categories_args', array(
'parent' => $item->object_id,
));
$sub_categories = get_categories($query_args);
$no_sub_categories = $sub_post = $postP = $post_thumb = $vnum_col = '';
$postID = url_to_postid( $item->url );
switch ($num_col) {
case "column-2":
$postP = '2';
break;
case "column-3":
$postP = '3';
break;
case "column-4":
$postP = '4';
break;
case "column-5":
$postP = '5';
break;
}
if($hany_megamenu_category == 'horizontal-cat'){
$tabclass = 'tabcat';
}elseif($hany_megamenu_category == 'vertical-cat'){
$tabclass = 'vert_cat';
}
if($tabclass == 'vert_cat'){
switch ($num_col) {
case "column-2":
$vnum_col = 'vcolumn-2';
$postP = '2';
break;
case "column-3":
$postP = '3';
$vnum_col = 'vcolumn-3';
break;
case "column-4":
$postP = '4';
$vnum_col = 'vcolumn-4';
break;
case "column-5":
$postP = '4';
$vnum_col = 'vcolumn-4';
break;
}
}else{
$vnum_col = '';
}
$sub_cat .= "<ul class="$tabclass">n";
$sub_cat .= "<li class=""><a href="$item->url" id="tabbox-$item->object_id" data-id="megacat-$item->object_id" class="badge badge-dark tabbox active">".esc_html__( 'All', HANY_TEXTDOMAIN )."</a></li>";
$cat_query = new WP_Query(array( 'cat' => $item->object_id, 'posts_per_page' => $postP ));
if($cat_query->have_posts()): 
$all_post .= "n<div class="megacatpost" id="megacat-$item->object_id"><ul>";
while($cat_query->have_posts()): $cat_query->the_post();
$post_titile = get_the_title();
if(has_post_thumbnail()){
$post_thumb = "<a href="".get_the_permalink()."" class="post-thumb FeauchThumbFig">
<img alt="$post_titile" src="" . get_the_post_thumbnail_url($postID, 'featured-image') . ""/></a>";
}else{
$post_thumb = '';
}
$all_post .= "n<li class="megamenu-column $num_col $vnum_col hany-animate-slideInUp hany-animate-delay">
$post_thumb
<a href="".get_the_permalink()."" class="">$post_titile</a>
<a class="mega-date"><span class="fa fa-clock-o" aria-hidden="true"></span> ".get_the_date()."</a>
</li>n";
endwhile;
$all_post .= "</ul></div>n";
endif;
$cat_childs = get_term_children( $item->object_id, 'category' );
foreach($cat_childs as $cat_child){
$childcat_name = get_cat_name( $cat_child );
$cat_link      = get_term_link( $cat_child, 'category' );
$category      = get_category($cat_child);
if($category->category_count > 0){
$sub_cat .= "<li class=""><a href="$cat_link" id="tabbox-$cat_child" data-id="megacat-$cat_child" class="badge badge-dark tabbox">$childcat_name</a></li>";
}
$the_query = new WP_Query(array( 'cat' => $cat_child, 'posts_per_page' => $postP ));
if($the_query->have_posts()): 
$sub_post .= "n<div class="megacatpost" id="megacat-$cat_child"><ul>";
while($the_query->have_posts()): $the_query->the_post();
$post_titile = get_the_title();
if(has_post_thumbnail()){
$post_thumb = "<a href="".get_the_permalink()."" class="post-thumb FeauchThumbFig">
<img alt="$post_titile" src="" . get_the_post_thumbnail_url($postID, 'featured-image') . ""/></a>";
}else{
$post_thumb = '';
}
$sub_post .= "n<li class="megamenu-column $num_col $vnum_col hany-animate-slideInUp hany-animate-delay">
$post_thumb
<a href="".get_the_permalink()."" class="">$post_titile</a>
<a class="mega-date"><span class="fa fa-clock-o" aria-hidden="true"></span> ".get_the_date()."</a>
</li>n";
endwhile;
$sub_post .= "</ul></div>n";
endif;
}
$sub_cat .= "</ul>n$all_postn$sub_post<div class="clear" id="Catclear"></div>";
}else{
$this->curCat = '';
$cat_col = '';
}
}
$output .= "n$indent<ul class="dropdown-menu$submenu depth_$depth" style='$back_img $pad_left_img $pad_right_img $min_height_img'>n";
//** display mega menu image background and styles --- end
if ($this->megaMenuID != 0 && $depth == 0) {
//display category start level
$output .= "$sub_cat";
$output .= "<li class="megamenu-column $num_col"><ul>n";
}
}
public function end_lvl(&$output, $depth = 0, $args = array())
{
$item = $this->curItem;
if ($this->megaMenuID != 0 && $depth == 0) {
$output .= "</ul></li>";
}
$output .= "</ul>";
}
public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
{
$hasMegaMenu      = get_post_meta( $item->ID, 'menu-item-mm-megamenu'      , true );
$hasColumnDivider = get_post_meta( $item->ID, 'menu-item-mm-column-divider', true );
$hasBoldTitle     = get_post_meta( $item->ID, 'menu-item-mm-column-title'  , true );
$hasDivider       = get_post_meta( $item->ID, 'menu-item-mm-divider'       , true );
$hasFeaturedImage = get_post_meta( $item->ID, 'menu-item-mm-featured-image', true );
$hasDescription   = get_post_meta( $item->ID, 'menu-item-mm-description'   , true );
$hasIcon          = get_post_meta( $item->ID, 'menu-item-mm-megamenu-icon' , true );
$this->curItem = $item;
$indent = ($depth) ? str_repeat("t", $depth) : '';
$li_attributes = '';
$class_names = $value = '';
$classes = empty($item->classes) ? array() : (array) $item->classes;
if ($this->megaMenuID != 0 && $this->megaMenuID != intval($item->menu_item_parent) && $depth == 0) {
$this->megaMenuID = 0;
}
if ($hasColumnDivider && $depth > 0) {
array_push($classes, 'column-divider');
$output .= "</ul></li><li class="megamenu-column ";
if($this->curColumn){
$output .= $this->curColumn;
}
$output .= ""><ul>n";
}
// managing divider: add divider class to an element to get a divider before it.
if ($hasDivider) {
$output .= "<li class="divider"></li>n";
}
if ($hasMegaMenu) {
array_push($classes, 'megamenu');
$this->megaMenuID = $item->ID;
}
if ($hasBoldTitle && $depth > 0 && $depth < 2) {
array_push($classes, 'boldtitle');
}
$classes[] = ($args->has_children || $item->object == 'category') ? 'dropdown' : '';
$classes[] = ($item->current || $item->current_item_ancestor) ? 'active' : '';
$classes[] = 'menu-item-'.$item->ID;
if ($depth && $args->has_children || $item->object == 'category') {
$classes[] = 'dropdown-submenu';
}
if ($hasFeaturedImage) {
array_push($classes, 'featured-image hany-animate-slideInUp hany-animate-delay');
}
if ($hasDescription) {
array_push($classes, 'description');
}
$class_names = implode(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
$class_names = ' class="'.esc_attr($class_names).'"';
$id = apply_filters('nav_menu_item_id', 'menu-item-'.$item->ID, $item, $args);
$id = strlen($id) ? ' id="'.esc_attr($id).'"' : '';
$output .= $indent.'<li'.$id.$value.$class_names.$li_attributes.'>';
$attributes = !empty($item->attr_title) ? ' title="'.esc_attr($item->attr_title).'"' : '';
$attributes .= !empty($item->target) ? ' target="'.esc_attr($item->target).'"' : '';
$attributes .= !empty($item->xfn) ? ' rel="'.esc_attr($item->xfn).'"' : '';
$attributes .= !empty($item->url) ? ' href="'.esc_attr($item->url).'"' : '';
// Add "nav-link" class for Boostrap V4 compatibility
$attributes .= ($args->has_children || $item->object == 'category') ? ' class="dropdown-toggle nav-link" data-toggle="dropdown"' : 'class="nav-link"';
// preview icon in frontend menu
$iconpicker_field = iconpicker_fields();
foreach ( $iconpicker_field as $custom_meta_field ){
$save_option_name   = str_replace( 'menu-item-', '', $custom_meta_field);
$save_option_name   = str_replace( '-', '_', $save_option_name);
$icon_value = get_post_meta( $item->ID, $save_option_name, true );
//Menu Item has an icon
if(! empty( $icon_value )){
$icon_preview = "<span aria-hidden="true" class="preview-menu-item-icon fa $icon_value"></span>n";
}else{
$icon_preview = '';
}
}
$item_output = $args->before;
$item_output .= '<a'.$attributes.'>'.$icon_preview;
// Check if item has featured image
if ($hasFeaturedImage && $this->megaMenuID != 0) {
$postID = url_to_postid( $item->url );
$item_output .= "<img alt="" . esc_attr($item->attr_title) . "" src="" . get_the_post_thumbnail_url( $postID, 'featured-image' ) . ""/>";
}
$item_output .= $args->link_before.apply_filters('the_title', $item->title, $item->ID).$args->link_after;
// add support for menu item title
if (strlen($item->attr_title) > 2) {
$item_output .= '<h3 class="tit">'.$item->attr_title.'</h3>';
}
// add support for menu item descriptions
if (strlen($item->description) > 2) {
$item_output .= '</a> <span class="sub">'.$item->description.'</span>';
}
$item_output .= (($depth == 0 || 1) && $args->has_children) ? ' <b class="caret"></b></a>' : '</a>';
$item_output .= $args->after;
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
public function display_element($element, &$children_elements, $max_depth, $depth, $args, &$output)
{
if (!$element) {
return;
}
// echo $element->object."<br/>";
$id_field = $this->db_fields['id'];
//display this element
if (is_array($args[0])) {
$args[0]['has_children'] = !empty($children_elements[$element->$id_field]);
} elseif (is_object($args[0])) {
$args[0]->has_children = !empty($children_elements[$element->$id_field]);
} 
$cb_args = array_merge(array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'start_el'), $cb_args);
$id = $element->$id_field;
// descend only when the depth is right and there are childrens for this element
if (($max_depth == 0 || $max_depth > $depth + 1 ) && isset($children_elements[$id])) {
foreach ($children_elements[ $id ] as $child) {
if (!isset($newlevel)) {
$newlevel = true;
//start the child delimiter
$cb_args = array_merge(array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
}
$this->display_element($child, $children_elements, $max_depth, $depth + 1, $args, $output);
}
unset($children_elements[ $id ]);
}
if (isset($newlevel) && $newlevel) {
//end the child delimiter
$cb_args = array_merge(array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
}
//end this element
$cb_args = array_merge(array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'end_el'), $cb_args);
}
}