在Wordpress中单击子菜单时,子菜单消失 [英] Submenu disappearing when clicked in the submenu in wordpress

查看:84
本文介绍了在Wordpress中单击子菜单时,子菜单消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在wordpress主题中为菜单和子菜单编写的服装功能,但是在我测试了子菜单后,单击子菜单后该子菜单消失了,因为wordpress不会将类别与子类别分开,因此用于它们是猫",这意味着当我单击一个子菜单时,创建该子菜单的函数会检查url中的cat = id是否具有子类别,但不是因为它是子类别,所以我是wordpress新手而且我不知道该如何处理:

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

推荐答案

您希望菜单看起来像什么?

What would you like your menu to look like?

  • 父类别a(当前),因此显示全部
    • 子类别a(当前),因此显示全部
      • Sub Sub cat
      • Parent category a (current) so show all
        • Child category a (current) so show all
          • Sub Sub cat

          如果这是最终结果,那么我们需要的是

          If this be the end result then what we need is to

          1. 从URL中获取category参数
          2. 实现一个递归函数以遍历所有类别.
          3. 重要的是,我们还需要获取该类别的后代,以判断是否 该菜单应显示其孩子是否已 已选择.
          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.

          这篇关于在Wordpress中单击子菜单时,子菜单消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆