Get current category (taxonomy) children and prepare them for options element
Author: Dimitar Radev | Posted on: August 13, 2018 |

Today I will share something that I use often. Getting the children of the current queried category or taxonomy and making from them an array containing slugs and titles (names).
This PHP function is very helpful when creating a <select> dropdown for subcategories filter for example.
<?php /** * Get current category (taxonomy) children and prepare them for options element. * * @return array */ function get_product_cat_children() { if (is_tax('product_category')) { $termID = get_queried_object()->term_id; $term_children = get_term_children( $termID, 'product_category' ); $children_arr = array(); foreach ($term_children as $child) { $term = get_term_by('id', $child, 'product_category'); var_dump($term); $child_val = $term->slug; $child_name =$term->name; $children_arr[] = array('val' => $child_val, 'name' => $child_name ); } return $children_arr; } }
Usage example
If you want to recieve the latest WordPress snippets, tutorials, tricks and useful information about web development, seo, social media marketing, google you can Subscribe to our newsletter. All you need to do is to fill the Subscribe form below, verify your Free Subscription in your email and start read our daily useful tips and tricks straight in your mailbox.
Note* we hate spam and our newsletter is powered by Google Feedburner and you will not get spam messages in your mailbox.
Leave a Reply