Submenu disappearing when clicked in the submenu i

2019-04-11 15:33发布

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

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;
  }
}

1条回答
小情绪 Triste *
2楼-- · 2019-04-11 16:00

What would you like your menu to look like?

  • Parent category 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 be the end result then what we need is to

  1. Grab the category parameter from the URL
  2. Implement a recursive function to loop through all the categories.
  3. Importantly we also need to get the descendent's of that category to be able to tell if that menu should show if its child or children's children have been selected.

More exact detail to follow if you wish.

查看更多
登录 后发表回答