有没有一种很好的方法可以将类别与wp中的子类别分开

时间:2012-03-22 17:50:47

标签: php wordpress parameters menu categories

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

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 个答案:

答案 0 :(得分:1)

尝试这样的功能:

function costume_submenu($cat) {
    $current_cat=get_category($cat);
    if($current_cat->parent==0)
        $parent_cat=$cat;
    else
        $parent_cat=$current_cat->parent;

    $categories =  get_categories("child_of=$parent_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;   
    } 
}