如何在wordpress中显示主要类别的子类别?

时间:2012-12-10 05:38:40

标签: wordpress

如何在wordpress中显示主要类别的子类别?喜欢 分类      子类别1      子类别2

2 个答案:

答案 0 :(得分: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"
  }
}

答案 1 :(得分: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']'");

}
?>