Submenu disappearing when clicked in the submenu in wordpress

浪尽此生 提交于 2019-12-06 06:58:35

问题


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:


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.



来源:https://stackoverflow.com/questions/9763497/submenu-disappearing-when-clicked-in-the-submenu-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!