2012-12-10 194 views

回答

0

你可以通过使用get_categories功能。像这样:

<?php 
    $cat = get_categories(array('child_of' => 3)); 
    var_dump($cat); 
?> 

child_of参数是父类别的ID。它将返回该父类别的所有子类别。

输出示例:

array(2) { 
    [0]=> 
    object(stdClass)#74 (15) { 
    ["term_id"]=> 
    &string(1) "4" 
    ["name"]=> 
    &string(19) "Child 1 of Parent 1" 
    ["slug"]=> 
    &string(16) "child-1-parent-1" 
    ["term_group"]=> 
    string(1) "0" 
    ["term_taxonomy_id"]=> 
    string(1) "4" 
    ["taxonomy"]=> 
    string(8) "category" 
    ["description"]=> 
    &string(0) "" 
    ["parent"]=> 
    &string(1) "3" 
    ["count"]=> 
    &string(1) "1" 
    ["cat_ID"]=> 
    &string(1) "4" 
    ["category_count"]=> 
    &string(1) "1" 
    ["category_description"]=> 
    &string(0) "" 
    ["cat_name"]=> 
    &string(19) "Child 1 of Parent 1" 
    ["category_nicename"]=> 
    &string(16) "child-1-parent-1" 
    ["category_parent"]=> 
    &string(1) "3" 
    } 
    [1]=> 
    object(stdClass)#114 (15) { 
    ["term_id"]=> 
    &string(1) "5" 
    ["name"]=> 
    &string(19) "Child 2 of Parent 1" 
    ["slug"]=> 
    &string(16) "child-2-parent-1" 
    ["term_group"]=> 
    string(1) "0" 
    ["term_taxonomy_id"]=> 
    string(1) "5" 
    ["taxonomy"]=> 
    string(8) "category" 
    ["description"]=> 
    &string(0) "" 
    ["parent"]=> 
    &string(1) "3" 
    ["count"]=> 
    &string(1) "1" 
    ["cat_ID"]=> 
    &string(1) "5" 
    ["category_count"]=> 
    &string(1) "1" 
    ["category_description"]=> 
    &string(0) "" 
    ["cat_name"]=> 
    &string(19) "Child 2 of Parent 1" 
    ["category_nicename"]=> 
    &string(16) "child-2-parent-1" 
    ["category_parent"]=> 
    &string(1) "3" 
    } 
} 
0

首先,你需要父类的id,然后你可以从表中它的子类wp_term_taxonomy

<?php global $wpdb;$prefix=$wpdb->prefix; 

$subcateogyr_list=$wpdb->get_results("Select * from ".$prefix."term_taxonomy WHERE parent='parent_category_id'"); 

foreach($subcateogyr_list as $subcat 

    echo $subcat_name=$wpdb->get_var("select name from ".$prefix."wp_terms where term_taxonomy_id='$subcat['term_id']'"); 

} 
?> 
相关问题