2012-03-22 163 views
1

中的子類別這裏是我爲我的wordpress主題中的菜單和子菜單編寫的服裝功能,但是當我測試它時,當我點擊子菜單時,子菜單消失了,因爲wordpress不會將類別從子類別中分離出來,因此它們的參數是「cat」,這意味着當我點擊子菜單時,創建子菜單的函數檢查url中的cat = id是否具有子類別,但它不是因爲它是子類,我是新進的WordPress,我不知道該如何處理它:有沒有一種很好的方法來分類wp

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

嘗試的功能是這樣的:

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

謝謝你的幫助@alesub,你寫的功能是解決我的問題 – TooCooL 2012-03-23 14:55:41

相關問題