SO网友:Rares P.
这可能会帮助您:
<?php
//first get the current term
$current_term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );
//then set the args for wp_list_categories
$args = array(
\'child_of\' => $current_term->term_id,
\'taxonomy\' => $current_term->taxonomy,
\'hide_empty\' => 0,
\'hierarchical\' => true,
\'depth\' => 1,
\'title_li\' => \'\'
);
wp_list_categories( $args );
?>
来源-
https://codex.wordpress.org/Function_Reference/get_term_by (您可以硬编码“books”分类法,也可以只获取活动分类法)。
EDITED
您可以创建一个自定义函数来获取分类法的子项,就像下面的链接一样。
$hierarchy = get_taxonomy_hierarchy( \'book\' );
/**
* Recursively get taxonomy hierarchy
*
* @param string $taxonomy
* @param int $parent - parent term id
* @return array
*/
function get_taxonomy_hierarchy( $taxonomy, $parent = 0 ) {
// only 1 taxonomy
$taxonomy = is_array( $taxonomy ) ? array_shift( $taxonomy ) : $taxonomy;
// get all direct decendents of the $parent
$terms = get_terms( $taxonomy, array( \'parent\' => $parent ) );
// prepare a new array. these are the children of $parent
// we\'ll ultimately copy all the $terms into this new array, but only after they
// find their own children
$children = array();
// go through all the direct decendents of $parent, and gather their children
foreach ( $terms as $term ){
// recurse to get the direct decendents of "this" term
$term->children = get_taxonomy_hierarchy( $taxonomy, $term->term_id );
// add the term to our new array
$children[ $term->term_id ] = $term;
}
// send the results back to the caller
return $children;
}
http://www.daggerhart.com/wordpress-get-taxonomy-hierarchy-including-children/