2016-03-04 12:36
采纳率: 100%
浏览 92

循环遍历嵌套数组的子元素wordpress get_terms和显示层次结构

I looked many guides over the net but nothing really completes the search what i need.

I am making custom taxonomy list for custom post type, and that taxonomy is hierarchical and should be basically displayed as category, but i want to point out subcategories or child items in select dropdown, something similarly like wordpress wp_list_categories, except i need custom output with only id and name because of dropdown list.

This is so far where i go, i managed to get them in line but as there are many subcategories i need to make it somehow dynamic.

enter image description here

I have found custom function which get whole list of categories and shift array to arrange them by parent-child relationship

function get_taxonomy_hierarchy( $taxonomy, $parent = 0 ) {
 // only 1 taxonomy
  $taxonomy = is_array( $taxonomy ) ? array_shift( $taxonomy ) : $taxonomy;

  // get all direct decendents of the $parent
  $terms = get_terms( $taxonomy, array( 'parent' => $parent, 'hide_empty' => false ) );

  // prepare a new array.  these are the children of $parent
  // we'll ultimately copy all the $terms into this new array, but only after they
  // find their own children
  $children = array();

 // go through all the direct decendents of $parent, and gather their children
  foreach ( $terms as $term ){
    // recurse to get the direct decendents of "this" term
   $term->children = get_taxonomy_hierarchy( $taxonomy, $term->term_id );

   // add the term to our new array
   $children[ $term->term_id ] = $term;

  // send the results back to the caller
  return $children;

And the output of that is next:

(sorry for posting on pastebin but array is really too long)

So from that i can see the child categories and output foreach parrent-child but i don't know how to out put it multi level and recursively, i just did it manually which is not quite good idea.

                        <select class="select2 form-control" data-placeholder="All Categories" data-allow-clear="true">
                        <?php $hierarchy = get_taxonomy_hierarchy( 'listings_category' );
                        foreach ($hierarchy as $list) {
                            echo '<option value="' . $list->term_id . '"><strong>' . $list->name . '</strong></option>';
                            if (!empty($list->children)) {
                               foreach ($list->children as $children) {
                                    echo '<option value="' . $children->term_id . '">- ' . $children->name . '</option>';
                                            if (!empty($children->children)) {
                                           foreach ($children->children as $subchildren) {
                                                echo '<option value="' . $subchildren->term_id . '">-- ' . $subchildren->name . '</option>';


So any ideas how can i make it dynamically and to have parent-child relationship

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • drws65968272 2016-03-04 13:07

    You can use wordpress inbuilt method wp_dropdown_categories()

    Here is the example..

    $args = [
                'show_option_all'    => 'All',
                'orderby'            => 'ID', 
                'order'              => 'ASC',
                'hide_empty'         => 0, 
                'selected'           => isset( $_REQUEST['your drop down name'] ) ? $_REQUEST['your drop down name'] : '' ,
                'hierarchical'       => 1, 
                'name'               => 'your drop down name',
                'taxonomy'           => 'your custom post type name',
                'hide_if_empty'      => false,
                'value_field'        => 'term_id',
            wp_dropdown_categories( $args );
    解决 无用
    打赏 举报

相关推荐 更多相似问题