doufu9836 2011-07-28 20:23
浏览 43

PHP wordpress获取类别对象的多维数组

am trying to get all the categories on my wordpress as a heirarchial multidimensional array of objects of type category. There are 2 functions in wordpress that kind of do what I want, but not exactly :

1) get_categories() - This one does return an array of category objects, which is great, but then the array is a flat one, where there is no differentiation between which category is a parent or which one is a child.

2) wp_list_categories() - which can return the categories with the herarchy intact, but problem is that the return value is html, and I need the categories to still be objects.

For example, if I had the following 3 trees as my parent-less categories :

/*
tree_stump

tree1
    branch1

tree2
    branch2
        twig2
            leaves2
            flowers2
            fruits2
*/

I am trying to get a return value which could be something like:

$cat_tree=array( 1=>$tree_stump,2=>array(1=>$tree1,2=>$branch1),3=>array(1=>$tree2,2=>array(1=>$branch2,2=>array($twig2,$leaves2,$flowers2,$fruits2  ) ) ) );

where each member of the multi dim array is still an object, and in all the child arrays, the first one is the parent of the rest of the members. I guess this would need some kinda recursive stuff - the stuff that gives me jitters !

Not sure if I made sense, please do let me know if I need to be more clear.. Thanks for reading.

  • 写回答

4条回答 默认 最新

  • duanmangxie7131 2011-07-28 20:28
    关注

    get_categories() returns an array with lots of properties you can use to build the array. namely $category->category_parent and $category->parent

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答