This problem occurs today when customizing the wordpress topic. I read the official function and did not explain the function get_category_children. Functionmy_get_category_children ($ id '', $ linktrue, $ separator '/', $ visitedarr
This problem occurs today when customizing the wordpress topic. I read the official function and did not explain the function get_category_children.
function my_get_category_children($id = '',$link = true,$separator = '/',$visited = array()){_deprecated_function( __FUNCTION__, '2.8', 'get_term_children()' );global $cat;if($id == '')$id = $cat;$chain = '';/** TODO: consult hierarchy */$cat_ids = get_all_category_ids();foreach ( (array) $cat_ids as $cat_id ) {if ( $cat_id == $id )continue;$category = get_category( $cat_id );if ( is_wp_error( $category ) )return $category;if ( $category->parent == $id && !in_array( $category->term_id, $visited ) ) {$visited[] = $category->term_id;$category_id = $category->term_id;$category_name = $category->name;$category_link = get_category_link( $category_id );if($link) $chain .= ''.$category_name.''.$separator;else $chain .= $category_name.$separator;$chain .=my_get_category_children( $category_id,$link,$separator,$visited );}}return $chain;}function my_the_category_children($id = '',$link = true,$separator = '/',$visited = array()){echo my_get_category_children($id,$link,$separator,$visited);}
There are two functions. The first is to return data, and the second is to display data. The function has four parameters: $ id and the ID of the current category. Based on this ID, the function finds the subcategory under the category and returns the result with the category name. $ Link is connected to the category address. $ separator is used after each category. because of my own theme requirements, I only set the connector after each category for the function, you can familiarize yourself with the function and modify it yourself. $ Visited plays a marking role in function running. you can also input values to exclude certain subcategories. Note that this parameter is an array and the array element is the ID of the category.