Submenu disappears when clicked on submenu in wordpress

Here are my costumed functions I wrote for menus and submenus in my wordpress theme, but after I tested it, the submenu disappeared when I clicked on the submenu, because Wordpress does not separate categories from subcategories, so the parameter for this is "cat "which means when I click on the submenu then the function that creates the submenu checks if the cat = id has child categories in the url, but this is not because it is a child category, I am new to wordpress and I don’t know how to deal with it:

function costume_menu() {
$categories =  get_categories('hide_empty=0&style=none&parent=0'); 
  foreach ($categories as $category) {
    (is_category($category->term_id)) ? $active = 'class="active_menu"' : $active = '';
    $nav = '<li>';
    $nav .= '<a '.$active.'href="'.get_category_link($category->term_id).'">'.strtoupper($category->cat_name).'</a>';
    $nav .= '</li>';

    echo $nav;
  }

}

function costume_submenu($cat) {

$categories =  get_categories("child_of=$cat&hide_empty=0"); 
  foreach ($categories as $category) {
    (is_category($category->term_id)) ? $active = 'class="active_menu"' : $active = '';
    $nav = '<li>';
    $nav .= '<a '.$active.'href="'.get_category_link($category->term_id).'">'.strtoupper($category->cat_name).'</a>';
    $nav .= '</li>';

    echo $nav;
  }
}

      

+3


source to share


1 answer


What would you like your menu to look like?

  • Parent category is a (current), so show all
    • Child category a (current), so show all
      • Sub Sub cat
  • Parent category b
  • Parent category c
  • Parent category d

If this is the end result, then we need



  • Take category parameter from url
  • Implement a recursive function to loop through all categories.
  • It is also important that the descendants of this category can know if this menu is to show if the child's children or children have been selected.

More precise detail to follow if you like.

+1


source







All Articles