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

时间:2016-03-04 12:36:34

标签: php arrays wordpress

我在网上看了很多指南但没有真正完成搜索我需要的东西。

我正在为自定义帖子类型制作自定义分类列表,并且该分类法是分层的,应该基本上显示为类别,但我想在选择下拉列表中指出子类别或子项目,类似于wordpress wp_list_categories,除了我需要由于下拉列表,只有id和name的自定义输出。

到目前为止,我去了,我设法将它们排成一行,但因为有许多子类别我需要让它以某种方式动态。

enter image description here

我找到了自定义函数,它获取了整个类别列表和移位数组,以通过父子关系来安排它们

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;
}

接下来的输出是:

(sorry for posting on pastebin but array is really too long)
http://pastebin.com/ekZAFb3C

所以我可以看到孩子类别和输出foreach parrent-child但我不知道怎么把它多层次和递归,我只是手动做了这不是一个好主意。

                        <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>

所以任何想法如何动态制作并拥有亲子关系

1 个答案:

答案 0 :(得分:1)

您可以使用wordpress内置方法wp_dropdown_categories()

以下是示例..

$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 );