get_category_children

Retrieve category children list separated before and after the term IDs.

Description

(string) get_category_children( (int) $id, (string) $before = '/', (string) $after = '', (array) $visited = array() ); 

Returns (string)

Parameters (4)

0. $id (int)
Category ID to retrieve children.
1. $before — Optional. (string) => '/'
Prepend before category term ID.
2. $after — Optional. (string) => ''
Optional, default is empty string. Append after category term ID.
3. $visited — Optional. (array) => array()
Category Term IDs that have already been added.

Usage

  1. if ( !function_exists( 'get_category_children' ) ) { 
  2. require_once ABSPATH . WPINC . '/deprecated.php'; 
  3.  
  4. // Category ID to retrieve children. 
  5. $id = -1; 
  6.  
  7. // Optional. Prepend before category term ID. 
  8. $before = '/'; 
  9.  
  10. // Optional, default is empty string. Append after category term ID. 
  11. $after = ''; 
  12.  
  13. // Optional. Category Term IDs that have already been added. 
  14. $visited = array(); 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = get_category_children($id, $before, $after, $visited); 
  18.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/deprecated.php  
  1. function get_category_children( $id, $before = '/', $after = '', $visited = array() ) { 
  2. _deprecated_function( __FUNCTION__, '2.8.0', 'get_term_children()' ); 
  3. if ( 0 == $id ) 
  4. return ''; 
  5.  
  6. $chain = ''; 
  7. /** TODO: consult hierarchy */ 
  8. $cat_ids = get_all_category_ids(); 
  9. foreach ( (array) $cat_ids as $cat_id ) { 
  10. if ( $cat_id == $id ) 
  11. continue; 
  12.  
  13. $category = get_category( $cat_id ); 
  14. if ( is_wp_error( $category ) ) 
  15. return $category; 
  16. if ( $category->parent == $id && !in_array( $category->term_id, $visited ) ) { 
  17. $visited[] = $category->term_id; 
  18. $chain .= $before.$category->term_id.$after; 
  19. $chain .= get_category_children( $category->term_id, $before, $after ); 
  20. return $chain;