function wpdm_categories_tree($parent = 0, $selected = array()) { $categories = get_terms('wpdmcategory', array('hide_empty' => 0, 'parent' => $parent)); $checked = ""; foreach ($categories as $category) { if ($selected) { foreach ($selected as $ptype) { if ($ptype->term_id == $category->term_id) { $checked = "checked='checked'"; break; } else { $checked = ""; } } } echo '<li><a href="' . get_term_link($category) . '"> ' . $category->name . ' </a>'; $termchildren = get_term_children($category->term_id, 'wpdmcategory'); if ($termchildren) { echo "<ul>"; wpdm_categories_tree($category->term_id, $selected); echo "</ul>"; } echo "</li>"; } }
/** * @param int|array $term_ids * * @return array|WP_Error */ function fw_ext_portfolio_get_listing_categories($term_ids) { $args = array('hide_empty' => false); if (is_numeric($term_ids)) { $args['parent'] = $term_ids; } elseif (is_array($term_ids)) { $args['include'] = $term_ids; } $ext_portfolio_settings = fw()->extensions->get('portfolio')->get_settings(); $taxonomy = $ext_portfolio_settings['taxonomy_name']; $categories = get_terms($taxonomy, $args); if (!is_wp_error($categories) && !empty($categories)) { if (count($categories) === 1) { $categories = array_values($categories); $categories = get_terms($taxonomy, array('parent' => $categories[0]->term_id, 'hide_empty' => false)); } foreach ($categories as $key => $category) { $children = get_term_children($category->term_id, $taxonomy); $categories[$key]->children = $children; //remove empty categories if ($category->count == 0 && (is_wp_error($children) || empty($children))) { unset($categories[$key]); } } return $categories; } return array(); }
/** * Get all product ids in a category (and its children) * * @param int $category_id * @return array */ public function get_products_in_category($category_id) { $term_ids = get_term_children($category_id, 'product_cat'); $term_ids[] = $category_id; $product_ids = get_objects_in_term($term_ids, 'product_cat'); return array_unique(apply_filters('woocommerce_report_sales_by_category_get_products_in_category', $product_ids, $category_id)); }
/** * @return array */ public function getChildIds() { if ($this->childIds === null) { $childIds = (array) get_term_children($this->id, $this->taxonomy); $this->childIds = array_map('intval', $childIds); } return $this->childIds; }
/** * @usage Check if a download manager category has child * @param $parent * @return bool */ function wpdm_cat_has_child($parent) { $termchildren = get_term_children($parent, 'wpdmcategory'); if (count($termchildren) > 0) { return true; } return false; }
public function getChildren() { $children = array(); print_r($this->_taxonomy); foreach (get_term_children($this->term_id, $this->taxonomy) as $term_id) { $children[] = static::forge($term_id, $this->taxonomy); } return $children; }
public function widget($args, $instance) { $title = apply_filters('widget_title', $instance['title']); echo $args['before_widget']; if (!empty($title)) { echo $args['before_title'] . $title . $args['after_title']; } $cat_args = array('parent' => $instance['parent'], 'number' => $instance['number'], 'hide_empty' => $instance['hide_empty'], 'orderby' => $instance['orderby'], 'order' => $instance['order']); $categories = get_terms('question_category', $cat_args); ?> <ul id="ap-categories-widget" class="ap-cat-wid clearfix"> <?php foreach ($categories as $key => $category) { $sub_cat_count = count(get_term_children($category->term_id, 'question_category')); ?> <li class="clearfix"> <a class="ap-cat-image" href="<?php echo get_category_link($category); ?> "><?php echo ap_get_category_image($category->term_id); ?> </a> <a class="ap-cat-wid-title" href="<?php echo get_category_link($category); ?> "> <?php echo $category->name; ?> </a> <div class="ap-cat-count"> <span><?php printf(_n('%d Question', '%d Questions', $category->count, 'categories-for-anspress'), $category->count); ?> </span> <?php if ($sub_cat_count > 0) { ?> <span><?php printf(__('%d Child', 'categories-for-anspress'), $sub_cat_count); ?> </span> <?php } ?> </div> </li> <?php } ?> </ul> <?php echo $args['after_widget']; }
function wcapf_get_term_childs($term_id, $taxonomy) { $transient_name = 'wcapf_term_childs_' . md5(sanitize_key($taxonomy) . sanitize_key($term_id)); if (false === ($term_childs = get_transient($transient_name))) { $term_childs = get_term_children($term_id, $taxonomy); set_transient($transient_name, $term_childs, WCAPF_CACHE_TIME); } return (array) $term_childs; }
function post_is_in_descendant_category($cats, $_post = null) { foreach ((array) $cats as $cat) { $descendants = get_term_children((int) $cat, 'category'); if ($descendants && in_category($descendants, $_post)) { return true; } } return false; }
public function is_in_descendant($post_id, $parent_term) { foreach ((array) $parent_term as $pt) { $pt = get_term_by('slug', $pt, 'category'); $child_terms = get_term_children($pt->term_id, $this->tax_name); if ($child_terms && in_category($child_terms, $post_id)) { return true; } } return false; }
/** * wpsc_products_by_category function. * * @access public * @param mixed $query * @return void */ function wpsc_products_by_category($query) { global $wpdb; $q = $query->query_vars; // Category stuff for nice URLs if (!empty($q['wpsc_product_category']) && !$query->is_singular) { $q['taxonomy'] = 'wpsc_product_category'; $q['term'] = $q['wpsc_product_category']; $in_cats = ''; $join = " INNER JOIN {$wpdb->term_relationships}\n\t\t\t\tON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)\n\t\t\tINNER JOIN {$wpdb->term_taxonomy}\n\t\t\t\tON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id)\n\t\t\t"; if (isset($q['meta_key'])) { $join .= " INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id) "; } $whichcat = " AND {$wpdb->term_taxonomy}.taxonomy = '{$q['taxonomy']}' "; $term_data = get_term_by('slug', $q['term'], $q['taxonomy']); if (is_object($term_data)) { $in_cats = array($term_data->term_id); } if ('0' != get_option('show_subcatsprods_in_cat') && is_object($term_data)) { $term_children_data = get_term_children($term_data->term_id, $q['taxonomy']); $in_cats = array_reverse(array_merge($in_cats, $term_children_data)); } if (is_array($in_cats)) { $in_cats = "'" . implode("', '", $in_cats) . "'"; $whichcat .= "AND {$wpdb->term_taxonomy}.term_id IN ({$in_cats})"; } $post_type_object = get_post_type_object('wpsc-product'); $permitted_post_statuses = current_user_can($post_type_object->cap->edit_posts) ? "'" . implode("', '", apply_filters('wpsc_product_display_status', array('publish'))) . "'" : "'publish'"; $whichcat .= " AND {$wpdb->posts}.post_status IN ({$permitted_post_statuses}) "; $groupby = "{$wpdb->posts}.ID"; $this->sql_components['join'] = $join; $this->sql_components['fields'] = "{$wpdb->posts}.*, {$wpdb->term_taxonomy}.term_id, {$wpdb->term_relationships}.term_order"; $this->sql_components['group_by'] = $groupby; //what about ordering by price if (isset($q['meta_key']) && '_wpsc_price' == $q['meta_key']) { $whichcat .= " AND {$wpdb->postmeta}.meta_key = '_wpsc_price'"; } else { $this->sql_components['order_by'] = "{$wpdb->term_taxonomy}.term_id"; // Term Taxonomy ID Ordering if ($q['orderby'] == 'menu_order') { if ($term_data) { $this->sql_components['order_by'] = "{$wpdb->term_relationships}.term_order ASC"; } } } $this->sql_components['where'] = $whichcat; add_filter('posts_join', array(&$this, 'join_sql')); add_filter('posts_where', array(&$this, 'where_sql')); add_filter('posts_fields', array(&$this, 'fields_sql')); add_filter('posts_orderby', array(&$this, 'order_by_sql')); add_filter('posts_groupby', array(&$this, 'group_by_sql')); } }
public function widget($args, $instance) { $title = apply_filters('widget_title', $instance['title']); echo $args['before_widget']; if (!empty($title)) { echo $args['before_title'] . $title . $args['after_title']; } $cat_args = array('parent' => $instance['parent'], 'number' => $instance['number'], 'hide_empty' => false, 'orderby' => $instance['orderby'], 'order' => $instance['order']); $categories = get_terms('question_category', $cat_args); ?> <ul class="ap-categories-list"> <?php foreach ($categories as $key => $category) { $sub_cat_count = count(get_term_children($category->term_id, 'question_category')); ?> <li> <a class="term-title ap-icon-category" href="<?php echo get_category_link($category); ?> "><?php echo $category->name; ?> <span><?php echo $category->count; ?> </span> <?php if ($sub_cat_count > 0) { ?> <i class="ap-icon-arrow-down"></i> <?php } ?> </a> <?php if ($sub_cat_count > 0) { echo '<div class="ap-term-sub">'; echo '<div class="sub-cat-count">' . $sub_cat_count . ' ' . __('Sub Categories', 'ap') . '</div>'; ap_child_cat_list($category->term_id); echo '</div>'; } ?> </li> <?php } ?> </ul> <?php echo $args['after_widget']; }
/** * @since 2.3 * @access private */ function _wpbdp_padded_count(&$term) { global $wpdb; $found = false; $count = intval(wp_cache_get('term-padded-count-' . $term->term_id, 'wpbdp', false, $found)); if (!$count && !$found) { $tree_ids = array_merge(array($term->term_id), get_term_children($term->term_id, WPBDP_CATEGORY_TAX)); $tt_ids = $wpdb->get_col($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id IN (" . implode(',', $tree_ids) . ") AND taxonomy = %s", WPBDP_CATEGORY_TAX)); $query = $wpdb->prepare("SELECT COUNT(DISTINCT r.object_id) FROM {$wpdb->term_relationships} r INNER JOIN {$wpdb->posts} p ON p.ID = r.object_id WHERE p.post_status = %s and p.post_type = %s AND term_taxonomy_id IN (" . implode(',', $tt_ids) . ")", 'publish', WPBDP_POST_TYPE); $count = apply_filters('_wpbdp_padded_count', intval($wpdb->get_var($query)), $term); } $term->count = $count; }
/** * Get the children types of given term. * * @param mixes $term Term ID (e.g. 12) or slug (e.g. 'creative-work') or name (e.g. 'CreativeWork'). * @param string $by Search key. Must be one of: 'id', 'slug', 'name', or 'term_taxonomy_id'. */ function wl_entity_type_taxonomy_get_term_children($term, $by = 'name') { // TODO: test this method // NOTE: WP taxonomy terms can have only one parent. This is a WP limit. $children_terms = array(); $term = get_term_by($by, $term, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME); if (isset($term->term_id)) { $children_ids = get_term_children($term->term_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME); foreach ($children_ids as $children_id) { $children_terms[] = get_term($children_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME); } } return $children_terms; }
function wp_all_export_check_children_assign($parent, $taxonomy, $term_ids = array()) { $is_latest_child = true; $children = get_term_children($parent, $taxonomy); if (count($children) > 0) { foreach ($children as $child) { if (in_array($child, $term_ids)) { $is_latest_child = false; break; } } } return $is_latest_child; }
protected function get_weekdays($instance) { global $week_mapping; $weekdays_cat = get_category_by_path(isset($instance['weekday_category']) ? $instance['weekday_category'] : 'timetable'); $weekdays = array_map(function ($t) { return get_category($t); }, get_term_children($weekdays_cat->term_id, $weekdays_cat->taxonomy)); $result = []; foreach ($weekdays as $weekday) { $index = $week_mapping[strtoupper($weekday->cat_name)]; $result[$index] = $weekday; } ksort($result); return $result; }
/** * @ticket 30335 */ public function test_should_rebuild_split_term_taxonomy_hierarchy() { global $wpdb; register_taxonomy('wptests_tax_3', 'post'); register_taxonomy('wptests_tax_4', 'post', array('hierarchical' => true)); $t1 = wp_insert_term('Foo1', 'wptests_tax_3'); $t2 = wp_insert_term('Foo1 Parent', 'wptests_tax_4'); $t3 = wp_insert_term('Foo1', 'wptests_tax_4', array('parent' => $t2['term_id'])); // Manually modify because shared terms shouldn't naturally occur. $wpdb->update($wpdb->term_taxonomy, array('term_id' => $t1['term_id']), array('term_taxonomy_id' => $t3['term_taxonomy_id']), array('%d'), array('%d')); $th = _get_term_hierarchy('wptests_tax_4'); $new_term_id = _split_shared_term($t1['term_id'], $t3['term_taxonomy_id']); $t2_children = get_term_children($t2['term_id'], 'wptests_tax_4'); $this->assertEquals(array($new_term_id), $t2_children); }
function ubik_places_list($term = null) { $tax = 'places'; // Allows us to pass an explicit term and achieve the same functionality if (empty($term) || $term == '') { $term = get_term_by('slug', get_query_var('term'), $tax); } // Check again if (!empty($term)) { $places = array(); // Get direct descendents of the current term $children = get_term_children($term->term_id, $tax); // Get direct descendents of the parent of the current term; get_term_children is not appropriate here $siblings = get_terms($tax, array('parent' => $term->parent)); // Get ancestors of the current term $ancestors = get_ancestors($term->term_id, $tax); // Get the highest ancestor of the current term if (!empty($ancestors)) { $patriarch = get_term(end($ancestors), $tax); } else { $patriarch = $term; } // Unite the whole family (the current term plus all ancestors) $family = $ancestors; $family[] = $term->term_id; // Setup children query if (!empty($children)) { // Attempt to limit terms with an abundance of children; this is pure guess work if (count($children) >= 25 && !empty($ancestors)) { $depth = 1; } else { $depth = 2; } $places[] = array('name' => 'children', 'title' => sprintf(__('Places in %s', 'ubik'), apply_filters('ubik_places_title', $term->name)), 'args' => array('child_of' => $term->term_id, 'depth' => $depth, 'show_count' => 1, 'hide_empty' => 0, 'taxonomy' => $tax, 'title_li' => '', 'show_option_none' => __('No places found', 'ubik'), 'echo' => 0)); // If there are no childen at least show the parent tree; no different than breadcrumbs, really } elseif (!empty($ancestors)) { $places[] = array('name' => 'ancestors', 'title' => sprintf(__('%s in context', 'ubik'), apply_filters('ubik_places_title', $term->name)), 'args' => array('depth' => 0, 'taxonomy' => $tax, 'include' => $family, 'title_li' => '', 'show_option_none' => __('No places found', 'ubik'), 'echo' => 0)); } // Setup sibling query; conditions: more than 2 siblings and not a top-level place if (!empty($siblings) && count($siblings) >= 2 && !empty($ancestors)) { $places[] = array('name' => 'siblings', 'title' => __('Related places', 'ubik'), 'args' => array('child_of' => $term->parent, 'depth' => 1, 'taxonomy' => $tax, 'exclude' => $term->term_id, 'title_li' => '', 'show_option_none' => __('No places found', 'ubik'), 'echo' => 0)); } // Places index $places[] = array('name' => 'index', 'title' => __('Places index', 'ubik'), 'args' => array('child_of' => 0, 'depth' => 1, 'show_count' => 1, 'taxonomy' => $tax, 'title_li' => '', 'show_option_none' => __('No places found', 'ubik'), 'echo' => 0)); } // Return the array of pseudo-widgets to the theme return $places; }
function joinCatChilds($value, $index) { $catid = get_term_by('name', $value, 'ad_cat'); $catid = $catid->term_id; $tax = 'tax' . $index; if (!empty($catid)) { (array) ($include_cats[] = $catid); $descendants = get_term_children((int) $catid, 'ad_cat'); foreach ($descendants as $k => $v) { $include_cats[] = $v; } $include_cats = '"' . implode('", "', $include_cats) . '"'; $join .= " AND {$tax}.term_id IN ({$include_cats}) "; } return $join; }
function tfuse_portfolio($atts) { extract(shortcode_atts(array('category' => '', 'columns' => 'three-column'), $atts)); wp_register_script('isotope', tfuse_get_file_uri('/js/isotope.pkgd.min.js'), array('jquery'), '1.0', true); wp_enqueue_script('isotope'); $output = ''; if ($category != 0) { $tax = 'group'; $term = get_term_by('id', $category, $tax); $term_children = get_term_children($category, $tax); $args = array('posts_per_page' => -1, 'post_type' => 'portfolio', 'tax_query' => array(array('taxonomy' => $tax, 'field' => 'id', 'terms' => $category))); $query = new WP_Query($args); $posts = $query->get_posts(); if (!empty($term)) { $output .= '<div class="shortcode-portfolio">'; if (tfuse_options('enable_portfolio_filter', true)) { $output .= '<div class="wrapp-categories-gallery"> <ul id="categories" class="gallery-categories">'; if (!empty($term_children)) { $output .= '<li class="categories-item active" data-category="' . $term->slug . '"><div class="check-category"><span></span></div>' . $term->name . '</li>'; foreach ($term_children as $id) { $term_child = get_term_by('id', $id, $tax); $output .= '<li class="categories-item" data-category="' . $term_child->slug . '"><div class="check-category"><span></span></div>' . $term_child->name . '</li>'; } } $output .= '</ul> <a class="prev" id="categories-prev" href="#"><i class="tficon-shevron-left"></i></a> <a class="next" id="categories-next" href="#"><i class="tficon-shevron-right"></i></a> </div>'; } $output .= '<section class="gallery">' . '' . ' <ul id="gallery-list" class="gallery-list ' . $columns . '">'; if (!empty($posts)) { foreach ($posts as $post) { $output .= '<li class="gallery-item" data-category="' . tfuse_get_portfolio_categories_list($post->ID) . '"> <div class="gallery-img">' . tfuse_get_portfolio_thumbnail($post->ID, $columns) . '</div> ' . tfuse_get_portfolio_gallery($post->ID) . ' </li>'; } } $output .= '</ul>'; $output .= '</section></div>'; } } return $output; }
function is_desc_cat($cats, $_post = null) { foreach ((array) $cats as $cat) { if (in_category($cat, $_post)) { return true; } else { if (!is_int($cat)) { $cat = get_cat_ID($cat); } $descendants = get_term_children($cat, 'category'); if ($descendants && in_category($descendants, $_post)) { return true; } } } return false; }
/** * Build a complete tree with the categories. Contains the complete tree for a given category and a children list for easy checking * * @param integer $category_id The category identifier we want to get the tree element for * * @return array $categories_list An array ordered by category with its children */ public static function category_tree($category_id = 0) { $categories_list = array(); $categories = get_terms(WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES, 'hide_empty=0&parent=' . $category_id); if (count($categories) > 0) { foreach ($categories as $category) { /* If necessary un-comment this line in order to get the complete tree for the category */ // $categories_list[$category->term_id]['children_tree'] = self::category_tree($category->term_id); $categories_list[$category->term_id]['children_category'] = get_term_children($category->term_id, WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES); /* Get the product list for the category */ $products = get_posts(array('post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES => $category->slug)); foreach ($products as $product) { $categories_list[$category->term_id]['children_product'][] = $product->ID; } } } return $categories_list; }
function trav_ajax_get_cities_in_country() { echo '<option></option>'; if (!empty($_POST['country_id'])) { $child_terms = get_term_children(sanitize_text_field($_POST['country_id']), 'location'); foreach ($child_terms as $term_id) { $term = get_term($term_id, 'location'); echo '<option value="' . esc_attr($term->term_id) . '">' . esc_html($term->name) . '</option>'; } } else { $terms = get_terms('location', array('hide_empty' => 0)); $terms = array_filter($terms, 'trav_check_term_depth_1'); foreach ($terms as $term) { echo '<option value="' . esc_attr($term->term_id) . '">' . esc_attr($term->name) . '</option>'; } } exit; }
function xs_all_faqs_get_category_tree($catID) { $results = array(); $childCategoryIDs = get_term_children($catID, 'category'); foreach ($childCategoryIDs as $childID) { $cat = get_term($childID, 'category'); $queryArgs = array('posts_per_page' => -1, 'post_type' => 'faq', 'tax_query' => array(array('taxonomy' => 'category', 'field' => 'term_id', 'terms' => $childID))); $query = new WP_Query($queryArgs); if ($query->found_posts > 0) { //$url = get_term_link($cat, 'category') $url = '/category-faq?wpv-category=' . $cat->slug; $results[] = array('id' => $cat->term_id, 'description' => $cat->description, 'name' => $cat->name, 'count' => $query->found_posts, 'url' => $url); } } usort($results, function ($a, $b) { return strcasecmp($a['description'], $b['description']); }); return $results; }
function thim_get_categories() { $args = array('taxonomy' => 'job_listing_category'); $items = array(); $items['all'] = 'All'; $categories = get_categories($args); if (isset($categories)) { foreach ($categories as $key => $cat) { $items[$cat->slug] = $cat->cat_name; $childrens = get_term_children($cat->term_id, $cat->taxonomy); if ($childrens) { foreach ($childrens as $key => $children) { $child = get_term_by('id', $children, $cat->taxonomy); $items[$child->slug] = '--' . $child->name; } } } } return $items; }
function thim_get_categories() { $args = array('orderby' => 'id', 'parent' => 0); $items = array(); $items['all'] = 'All'; $categories = get_categories($args); if (isset($categories)) { foreach ($categories as $key => $cat) { $items[$cat->cat_ID] = $cat->cat_name; $childrens = get_term_children($cat->term_id, $cat->taxonomy); if ($childrens) { foreach ($childrens as $key => $children) { $child = get_term_by('id', $children, $cat->taxonomy); $items[$child->term_id] = '--' . $child->name; } } } } return $items; }
public function start_el(&$output, $category, $depth = 0, $args = array(), $id = 0) { /** This filter is documented in wp-includes/category-template.php */ $cat_name = apply_filters('list_cats', esc_attr($category->name), $category); // Don't generate an element if the category name is empty. if (!$cat_name) { return; } $wpb_wmca_cat_count = ''; if (!empty($args['show_count'])) { $wpb_wmca_cat_count = '<span class="wpb-wmca-cat-count">' . number_format_i18n($category->count) . '</span>'; } $link = '<a href="' . esc_url(get_term_link($category)) . '" '; if ($args['use_desc_for_title'] && !empty($category->description)) { $link .= 'title="' . esc_attr(strip_tags(apply_filters('category_description', $category->description, $category))) . '"'; } $link .= '>'; $link .= $cat_name . $wpb_wmca_cat_count . '</a>'; if ('list' == $args['style']) { $output .= "\t<li"; $css_classes = array('cat-item', 'cat-item-' . $category->term_id); $termchildren = get_term_children($category->term_id, $category->taxonomy); if (count($termchildren) > 0) { $css_classes[] = 'cat-item-have-child'; } if (!empty($args['current_category'])) { $_current_category = get_term($args['current_category'], $category->taxonomy); if ($category->term_id == $args['current_category']) { $css_classes[] = 'current-cat'; } elseif ($category->term_id == $_current_category->parent) { $css_classes[] = 'wpb-wmca-current-cat-parent'; } } $css_classes = implode(' ', apply_filters('category_css_class', $css_classes, $category, $depth, $args)); $output .= ' class="' . $css_classes . '"'; $output .= ">{$link}\n"; } else { $output .= "\t{$link}<br />\n"; } }
/** * Encodes the list of terms in a given taxonomy into an array variable. * Choose between array, comma-separated string or slug. */ function sandwich_functions_term_list($taxonomyName = 'post_tag') { $terms = get_terms($taxonomyName, array('parent' => 0, 'hide_empty' => false)); $output = array(0 => sprintf('— %s —', __('Select', 'pbsandwich'))); if (is_wp_error($terms)) { return $output; } foreach ($terms as $term) { $output[$term->slug] = $term->name; $term_children = get_term_children($term->term_id, $taxonomyName); if (is_wp_error($term_children)) { continue; } foreach ($term_children as $term_child_id) { $term_child = get_term_by('id', $term_child_id, $taxonomyName); if (is_wp_error($term_child)) { continue; } $output[$term_child->slug] = $term_child->name; } } return $output; }
public function atmf_get_child_taxonomy() { extract($_POST); $terms_parent_id = array(); $terms_parent_array = array(); $parent_taxonomy_output = array(); $data = array(); if (empty($_POST['parent'])) { $terms = get_terms($taxonomy, 'orderby=count&hide_empty=0&parent=0'); foreach ($terms as $term) { $parent_taxonomy_output['alloption'][$term->term_id] = $term->name; if (!empty($term->parent)) { $terms_parent_id[] = $term->parent; } $data[$term->term_id] = get_term_children($term->term_id, $taxonomy); } $parent_terms = get_terms($taxonomy, array('include' => $terms_parent_id, 'parent' => 0, 'hide_empty' => false)); foreach ($parent_terms as $key => $value) { if (!empty($data[$value->term_id])) { $parent_taxonomy_output['first_parent'][$value->term_id] = $value->name; } } } else { $terms = get_terms($taxonomy, array('parent' => $parent, 'hide_empty' => false)); foreach ($terms as $key => $value) { $parent_taxonomy_output['alloption'][$value->term_id] = $value->name; $data[$value->term_id] = get_term_children($value->term_id, $taxonomy); } foreach ($terms as $key => $value) { if (!empty($data[$value->term_id])) { $parent_taxonomy_output['child_parent'][$value->term_id] = $value->name; } } } echo json_encode($parent_taxonomy_output, JSON_NUMERIC_CHECK); wp_die(); }
function nestedCategories($cat) { if (count($cat) > 0) { foreach ($cat as $c) { $link = get_term_link($c); echo '<a class="ui basic button" href="' . $link . '" style="font-weight:bold;margin-top: 5px;">'; echo $c->name; $taxonomy_name = $c->taxonomy; echo $c->term_id; echo '</a>'; $parent_id = $c->term_id; $subcategories = get_terms('taxonomy_name', array('child_of' => $parent_id, 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC')); //then set the args for wp_list_categories $termchildren = get_term_children($c->term_id, $taxonomy_name); if ($termchildren) { echo '<br>'; foreach ($termchildren as $child) { $term = get_term_by('id', $child, $taxonomy_name); echo '<a href="' . get_term_link($child, $taxonomy_name) . '" class="ui basic button" style="margin-top: 5px;">' . $term->name . '</a>'; } } } } }