嵌套自定义分类|查询时发布错误

时间:2016-11-22 作者:Vladros

我在wordpress中有两个自定义分类法,它们附带了帖子,如下例所示:

Taxanomy A
Taxanomy B

1号岗位
2号岗位

根据需要,每个帖子都附带了两个分类法。我需要输出每个分类法A都有分类法B,每个分类法B都有X个帖子。

我想在数组中实现这一点,最终以JSON格式输出,这就是我设法编写和使用的内容,但结果不正确:

$TaxanomiesA = get_terms("TaxanomyA", array(\'hide_empty\' => false));
$TaxanomiesB = get_terms("TaxanomyB", array(\'hide_empty\' => false));

$TaxanomyAArray = array();
$TaxanomyBArray = array();
$postssArray = array();

foreach($TaxanomiesA as $TaxanomyA) {
        $TaxanomyAArray[] = array (  
        \'id\' => $TaxanomyA->term_id,
        \'name\' => $TaxanomyA->name,
        \'TaxanomyB\' => $TaxanomyBArray
    );

    $args = array(//Arguments);
    $posts = get_posts( $args );

        foreach($TaxanomiesB as $TaxanomyB){
                $TaxanomyBArray[] = array (  
                        \'id\' => $TaxanomyB->term_id,
                        \'name\' => $TaxanomyB->name,
                        \'posts\' => $postssArray
                    );
            foreach($posts as $post){
                    $postssArray[] = array (  
                        \'id\' => $post->ID,
                        \'name\' => $post->title
                    );
            }

        }

    }  

    $jsonOutput=array(
        \'JSON\' => $TaxanomyAArray
    );

    echo json_encode($jsonOutput, JSON_UNESCAPED_SLASHES);                                            

}
这是我想要实现的输出:

{
   "JSON":[
      {
         "id":"1",
         "name":"TaxonomyA Name",
         "Taxonomy":[
            {
               "id":"1",
               "name":"TaxonomyB Name",
               "posts":[
                  {
                     "id":"1",
                     "name":"Post Title"
                  },
                  {
                     "id":"2",
                     "name":"Post Title 2"
                  }
               ]
            }
         ]
      }
   ]
}

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

问题是get_posts 正在外部调用函数$TaxanomiesB 环把它移到里面,确保电流$TaxanomyB 正在中的$args中使用get_posts.

将数据添加到$TaxanomyAArray$TaxanomyBArray 在每个for-each循环的末尾,否则数组中的第一个对象将为空。

您还需要重新初始化$array 变量位于每个父循环的开头,否则最终会在每个集合中多次重复每个帖子/分类法(如\'a\' => (1 => (foo, bar), 2 => (foo, bar, foo, bar)), \'b\' => 1 => (foo, bar, foo, bar, foo, bar) ...等等)。

$TaxanomiesA = get_terms("TaxanomyA", array(\'hide_empty\' => false));
$TaxanomiesB = get_terms("TaxanomyB", array(\'hide_empty\' => false));

$TaxanomyAArray = array();

foreach($TaxanomiesA as $TaxanomyA) {
    $TaxanomyBArray = array();
    foreach($TaxanomiesB as $TaxanomyB){
        $args = array(//Arguments with $TaxonomyB);
        $posts = get_posts( $args );
        $postsArray = array();
        foreach($posts as $post){
            $postsArray[] = array (  
                \'id\' => $post->ID,
                 \'name\' => $post->title
            );
        }
        $TaxanomyBArray[] = array (  
            \'id\' => $TaxanomyB->term_id,
            \'name\' => $TaxanomyB->name,
            \'posts\' => $postsArray
        );
    }

    $TaxanomyAArray[] = array (  
        \'id\' => $TaxanomyA->term_id,
        \'name\' => $TaxanomyA->name,
        \'TaxanomyB\' => $TaxanomyBArray
    );
}  

$jsonOutput = array(
     \'JSON\' => $TaxanomyAArray
);

echo json_encode($jsonOutput, JSON_UNESCAPED_SLASHES);