dsdeeaquu38538545 2016-03-04 12:36 采纳率: 100%
浏览 97
已采纳

循环遍历嵌套数组的子元素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)
http://pastebin.com/ekZAFb3C

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">
                        <option></option> 
                        <?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>';
                                           }
                                        }
                               }
                            }
                        }

                        ?>
                    </select>

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 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献