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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀