JSON file export of nav menu

时间:2014-10-16 作者:Nagendra Rao

我试图用json符号导出wordpress站点菜单的精华。

我想提取一些细节,如id、类型、名称、子项,并想构建一个Json对象,如下所示。

项目-

{
  id: \'wp entity id\',
  name: \'name of the category|taxonomy|post|page\',
  type: \'post|category|page\',
  children: [array of Items]
}
其中,

“子项”:此处表示子项“类型”:表示项的类型,即帖子链接、页面链接、类别链接和自定义链接

编辑:我试图实现的示例,假设我有如下菜单,

-0-------1--------2-------3+- level/depth
 |
 |\\___ Lobby
 |
 |\\___ Projects
 |       |\\___ Project A
 |       |       |\\___ Review
 |       |       |\\___ Comments
 |       |       \\____ Download
 |       \\____ Project B
 |               |\\___ Review
 |               |\\___ Comments
 |               \\____ Download
 |\\___ Blog
 |
 \\____ About
         |\\___ Legal
         \\____ Contact
我想将此菜单表示为对象。

{
    {
      id: \'1\',
      name: \'lobby\',
      type: \'page\',
      children: []
    },
    {
      id: \'12\',
      name: \'Projects\',
      type: \'category\',
      children: [{
                   id: \'12\',
                   name: \'Project A\',
                   type: \'category\',
                   children: [ ... ]
                }]
    },
  ...
  ...
    {
      id: \'15\',
      name: \'blog\',
      type: \'page\',
      children: []
    },

  ...

}

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

找到一个解决方案,修改它以满足我的需要,

菜单项类:

class Menu_Item{
    private $object;
    private $object_id;
    private $link_name;
    private $link_value;

    public function __construct($object, $object_id, $linkname, $linkvalue){
      $this->object_id = $object_id;
      $this->object = $object;
      $this->link_name = $linkname;
      $this->link_value = $linkvalue;
    }

    public function get_link_name(){
      return $this->link_name;
    }

    public function get_object_id(){
      return $this->object_id;
    }

    public function get_link_value(){
      return $this->link_value;
    }

    public function get_object(){
      return $this->object;
    }
}
JSON生成器:

$menu_name = \'main-menu\'; //menu name slug
$menu_json_data_string = \'{\';

if ($menu_name) {
  $menu = wp_get_nav_menu_object( $menu_name );

  $menu_items = wp_get_nav_menu_items($menu->term_id);

  //initialize two menu item arrays
  $main_menu_item_array = array();
  $sub_menu_item_array = array();//defined as 2d array
  //define two menu item arrays first
  for($i=0;$i<count($menu_items);$i+=1){
    $current_menu_item_obj = $menu_items[$i];

    if($current_menu_item_obj->menu_item_parent == "0"){ //main menu item found
      //define a new menu_item object
      $menu_item_obj = new Menu_Item($current_menu_item_obj->object, $current_menu_item_obj->object_id, $current_menu_item_obj->title, $current_menu_item_obj->url);
      $main_menu_item_array[$current_menu_item_obj->ID] = $menu_item_obj;
    }else{ //sub menu found
      $menu_item_obj = new Menu_Item($current_menu_item_obj->object, $current_menu_item_obj->object_id, $current_menu_item_obj->title, $current_menu_item_obj->url);
      $sub_menu_item_array[$current_menu_item_obj->menu_item_parent][] = $menu_item_obj;
    }
  }

  $menu_item_json_array = array();
  foreach($main_menu_item_array as $each_main_menu_key=>$each_main_menu_item){
    $each_menu_item_link_name = $each_main_menu_item->get_link_name();
    $each_menu_item_link_value = $each_main_menu_item->get_link_value();
    $each_menu_item_object_id = $each_main_menu_item->get_object_id();
    $each_menu_item_slug = strtolower(str_replace(" ", "_", $each_menu_item_link_name));
    $obj = $each_main_menu_item->get_object();
    $type = ($obj==\'meta_category\')?\'meta\':\'main\';
    $each_menu_item_string = \'"\'.$each_menu_item_slug.\'":{"id": "\'.$each_menu_item_object_id.\'", "type": "\'.$type.\'", "url":"\'.$each_menu_item_link_value.\'","name":"\'.$each_menu_item_link_name.\'"\';

    if(array_key_exists($each_main_menu_key, $sub_menu_item_array)){ //has sub menu item
      //count submenu items
      $count_sub_menu_item = count($sub_menu_item_array[$each_main_menu_key]);
      //put each sub menu item into an array
      $submenu_item_list_main_string = \',"children": [\';
      $submenu_item_list_array = array();
      foreach($sub_menu_item_array[$each_main_menu_key] as $each_sub_menu_item){
        $obj = $each_sub_menu_item->get_object();
        $type = ($obj==\'meta_category\')?\'meta\':\'main\';
        $each_sub_menu_item_string = \'{"id": "\'.$each_sub_menu_item->get_object_id().\'", "type": "\'.$type.\'", "url":"\'.$each_sub_menu_item->get_link_value().\'", "name":"\'.$each_sub_menu_item->get_link_name().\'"}\';
        $submenu_item_list_array[] = $each_sub_menu_item_string;
      }
      $submenu_item_list_string = implode(",", $submenu_item_list_array);
      $submenu_item_list_main_string .= $submenu_item_list_string;
      $submenu_item_list_main_string .= \']\';
      $each_menu_item_string .= $submenu_item_list_main_string;
    }
    $each_menu_item_string .= \'}\';
    $menu_item_json_array[] = $each_menu_item_string;
  }
  $menu_item_json_string = implode(",", $menu_item_json_array);
}

$menu_json_data_string .= $menu_item_json_string;
$menu_json_data_string .= \'}\';

echo $menu_json_data_string;
参考号:http://simple2kx.com/wordpress-create-json-data-page-to-load-menu-items/

结束

相关推荐

How to add taxonomy in menus?

书籍(自定义帖子类型)小说(税)科学(税)历史(税)--书籍体裁(税务)小说(术语)科学(学期)历史(学期)以下哪一项是做这件事的“好方法”?对于前一个(这是我目前在管理菜单中的功能,我为每个功能都提供了“register\\u taxonomy”功能),我无法选择要在菜单中显示的“Tax”。而对于后者,我可以将它们添加到菜单中,只需要一个“register\\u taxonomy”函数。