私が開発しているワードプレス Web サイトでは、ユーザーが管理メニューを使用して作成できる動的メニューを作成しています。それにフックすることは、私の問題の中で最も小さいものです。
私が使用しているコードは、これらの配列を返します。
Array
(
[0] => WP_Post Object
(
[ID] => 35
[post_author] => 1
[post_date] => 2013-05-19 15:46:22
[post_date_gmt] => 2013-05-19 15:46:22
[post_content] =>
[post_title] =>
[post_excerpt] =>
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => 35
[to_ping] =>
[pinged] =>
[post_modified] => 2013-05-19 16:07:09
[post_modified_gmt] => 2013-05-19 16:07:09
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://adapt.local/?p=35
[menu_order] => 1
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 35
[menu_item_parent] => 0
[object_id] => 32
[object] => training
[type] => post_type
[type_label] => Training
[url] => http://adapt.local/training/alcohol/
[title] => Alcohol
[target] =>
[attr_title] =>
[description] =>
[classes] => Array
(
[0] =>
)
[xfn] =>
)
[1] => WP_Post Object
(
[ID] => 36
[post_author] => 1
[post_date] => 2013-05-19 16:07:09
[post_date_gmt] => 2013-05-19 16:07:09
[post_content] =>
[post_title] =>
[post_excerpt] =>
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => 36
[to_ping] =>
[pinged] =>
[post_modified] => 2013-05-19 16:07:09
[post_modified_gmt] => 2013-05-19 16:07:09
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://adapt.local/?p=36
[menu_order] => 2
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 36
[menu_item_parent] => 35
[object_id] => 32
[object] => training
[type] => post_type
[type_label] => Training
[url] => http://adapt.local/training/alcohol/
[title] => Alcohol
[target] =>
[attr_title] =>
[description] =>
[classes] => Array
(
[0] =>
)
[xfn] =>
)
)
説明: menu_item_parent = 0 の場合、それはトップノードであることを意味し、menu_item_parent > 0 の場合、それは「サブノード」であることを意味します。
この恐ろしい配列をより便利なもの、できればこのようなものに変換したい
Array
(
[35] => Array
(
name => "Topnode"
url => "http://topnodeurl"
items => Array
(
name => "Subnode"
url => "http://subnodeurl"
)
)
)
私は思った:ねえ、これはそれほど難しいことではなかった. しかし、どうやら、これはうまくいかないようです:
foreach($menuitems as $menuitem) {
if(!$menuitem->menu_item_parent) {
$items[$menuitem->ID] = array("name" => $menuitem->title,"items" => array());
#print_r($items);
}
else {
$parent = $items[$menuitem->menu_item_parent]['items'];
$parent = array("name" => $menuitem->title, "url" => $menuitem->url);
}
}
何か案は?