0

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

4

2 回答 2

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"
  }
}
于 2012-12-10T06:21:21.763 回答
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']'");

}
?>
于 2016-04-18T10:10:08.900 回答