The custom get_category_children function obtains a subcategory based on the Category ID.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.