使用分类法帮助按层次构建数组(以获取帖子)

时间:2013-11-26 作者:Simon

我需要做的是:

-+检索分配给页面的所有术语-+获取分配给每个术语的所有帖子-+以层次结构的方式组织输出,如管理

我对前两项没什么意见,但我似乎找不到一种方法来以正确的分层方式组织输出(可能是因为我不擅长数组,也可能是PHP)。

这就是我现在得到的:

public function return_all_tax_objects() {

    $obj_term_posts = array();

    foreach ( $this->return_object_terms() as $obj_term ) {

        if ( $obj_term->parent ) {
            unset( $key );
            $ancestors = array_reverse( get_ancestors( $obj_term->term_id, $obj_term->taxonomy ) );
            foreach ( $ancestors as $ancestor ) {
                $ancestor_term = get_term( $ancestor, $obj_term->taxonomy );
                $key .= "[$ancestor_term->slug]";
            }
            $key .= "[$obj_term->slug]";
        }

        if ( array_key_exists( $key, $obj_term_posts ) ) {

             $get_term_posts = $this->get_posts_for_term( $obj_term );
             array_push ( $obj_term_posts[ $key ], $this->get_posts_for_term( $obj_term ) );

        } else {

            $obj_term_posts[ $key ] = $this->get_posts_for_term( $obj_term );

        }

    }
    return $obj_term_posts;
}
以及$this->return_object_terms() 对应于返回ID 对于我设置的所有自定义帖子类型,在平面数组中:

public function return_object_terms() {

    return wp_get_object_terms( $this->object_id, $this->return_object_taxonomies(), array ( \'orderby\' => \'term_id\', \'order\' => \'ASC\', \'fields\' => \'all\' ) );
}
正如您将在我的快照中看到的返回值,我很接近,但:

Edit : 更改了代码以获得更好的选项。但还是不行。

不知怎么的,我把一切都搞混了。我有正确的帖子,但我没有更新正确的索引somwhere。

本质上,我需要的是一种将子项嵌套到父项中的方法,这样我就可以构建输出:

-+ Direction générale des services
--+ (posts)
---+ Services Techniques
---+ (posts)
----+ Directeur des Services Techniques
----+ (posts)
---+ Services Administratifs
---+ (posts)
...
Firebug capture of returned array $obj_term_posts

1 个回复
最合适的回答,由SO网友:Simon 整理而成

我成功了!

这是返回基于分类法的分层数组的最后一段代码。

public function return_all_tax_objects() {

    $obj_term_posts = array();

            // GET FLAT LIST OF TERMS BELONGING TO THE POST
    foreach ( $this->return_object_terms() as $obj_term ) {

                    // IF TERM HAS A PARENT
        if ( $obj_term->parent ) {
            unset ( $keys );
                            // GET ANCESTORS OF TERM AND REVERSE IT 
                            // SO WE HAVE IT FROM ROOT
            $ancestors = array_reverse( get_ancestors( $obj_term->term_id, $obj_term->taxonomy ) );
                            // WE GET ANCESTORS AND STORE SLUG IN ARRAY
            foreach ( $ancestors as $ancestor ) {
                $ancestor_term = get_term( $ancestor, $obj_term->taxonomy );
                $keys[] = $ancestor_term->slug;
            }
        }

                    // LAST ITEM FOR KEYS IS CURRENT TERM SLUG
        $keys[] = $obj_term->slug;

                    // DUPLICATE WITH INCLUSION 
        $temp =& $obj_term_posts;

                    // CREATE INDEX FROM KEYS ARRAY
        foreach ( $keys as $key ) {
            $temp =& $temp[ $key ];
        }
        // GET ASSOCIATED POSTS TO THAT TERM 
        $temp = $this->get_posts_for_term( $obj_term );

    }

    return $obj_term_posts;
}
结果:enter image description here

结束

相关推荐

rewrite rules hierarchical

我希望我的WordPress遵循以下规则:/productes/ 包含所有产品的页面/productes/[category]/ 包含该类别所有产品的分类页面/productes/[category]/[subcategory]/ 包含该类别所有产品(与2相同)的分类页面/[productes]/[category]/[product]/ A.single-productes.php 将显示类别产品/[productes]/[category]/[subcategory]/[product]/ A.sin