Created
August 25, 2017 14:43
-
-
Save phamngsinh/f892b3c25912d57b1ada9b1db17ec830 to your computer and use it in GitHub Desktop.
wordpress get children of category
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| $term_id = 1; | |
| $taxonomy_name = 'category'; | |
| $args = array( 'parent' => $term_id); | |
| $termchildren = get_product_children($term_id, $taxonomy_name); | |
| **/ | |
| function get_product_children( $term_id, $taxonomy ) { | |
| if ( ! taxonomy_exists( $taxonomy ) ) { | |
| return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); | |
| } | |
| $term_id = intval( $term_id ); | |
| $terms = _get_term_hierarchy($taxonomy); | |
| if ( ! isset($terms[$term_id]) ) | |
| return array(); | |
| $children = $terms[$term_id]; | |
| foreach ($children as $key=>$child ) { | |
| unset($children[$key]); | |
| $children[$child] = get_term_children($child, $taxonomy); | |
| } | |
| return $children; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment