admin管理员组文章数量:1122832
I'm attempting to read a JSON file that was was successfully exported using wp_send_json_success
however I'm unable to loop through the array in the file since it seems that I can't access $menu->terms
. What am I overlooking?
$json_content = file_get_contents( ABSPATH . '/menu.json', true );
$menus = json_decode( $json_content );
if ( ! empty( $menus ) ) {
foreach ( $menus as $menu ) {
// Get the internal menu by slug.
$internal_menu = get_term_by( 'slug', 'primary', 'nav_menu' );
// Check if a menu with the specified name already exists.
$menu_exists = wp_get_nav_menu_object( 'Primary' );
if ( $menu_exists ) {
// If the menu exists, delete it.
wp_delete_nav_menu( 'Primary' );
}
// Create a new menu with the same name.
$menu_id = wp_create_nav_menu( 'Primary' );
// Build arguments for new menu items.
$args = array();
$index = 0;
if ( ! empty( $menu->terms ) ) {
foreach ( $menu->terms as $menu_item ) {
// Skip if the post status is 'draft'.
if ( 'draft' === $menu_item->post_status ) {
continue;
}
// Process menu item based on its type.
$args_menu_item = process_menu_item( $menu_item );
// Update nav menu item and get the updated ID.
$nav_menu_item_update = wp_update_nav_menu_item( $menu_id, 0, $args_menu_item );
// Update the object ID in the arguments.
$args_menu_item['menu-item-object-id'] = $nav_menu_item_update;
$args[ $index++ ] = $args_menu_item;
}
}
// Update parent ID for menu items.
update_parent_id( $menu_id, $args );
}
}
}
Here's an excerpt from the JSON file that $menu
spits out
{
"success": true,
"data": [
{
"term_id": 4,
"name": "Primary",
"slug": "primary",
"term_group": 0,
"term_taxonomy_id": 4,
"taxonomy": "nav_menu",
"description": "",
"parent": 0,
"count": 31,
"filter": "raw",
"terms": [
{
"ID": 269,
"post_author": "1",
"post_date": "2023-06-27 03:46:59",
"post_date_gmt": "2023-01-01 03:56:36",
"post_content": "…………………………………………",
"post_title": "",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "269",
"to_ping": "",
"pinged": "",
"post_modified": "2023-06-27 03:46:59",
"post_modified_gmt": "2023-06-27 03:46:59",
"post_content_filtered": "",
"post_parent": 0,
"guid": "/?p=269",
"menu_order": 1,
"post_type": "nav_menu_item",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": 269,
"menu_item_parent": "0",
"object_id": "2",
"object": "page",
"type": "post_type",
"type_label": "Front Page",
"url": "/",
"title": "Home",
"target": "",
"attr_title": "",
"description": "……………………………………………",
"classes": [
""
],
"xfn": "",
"menu_item_parent_object_id": "blank",
"menu_item_parent_type": "blank",
"menu_item_parent_title": "blank",
"menu_item_parent_url": "blank",
"menu_item_parent_id": "blank",
"menu_item_parent_post_name": "blank"
},
{
"ID": 277,
"post_author": "1",
"post_date": "2023-06-27 03:46:59",
"post_date_gmt": "2023-01-01 04:01:38",
"post_content": "…………………………………………",
"post_title": "About",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "277",
"to_ping": "",
"pinged": "",
"post_modified": "2023-06-27 03:46:59",
"post_modified_gmt": "2023-06-27 03:46:59",
"post_content_filtered": "",
"post_parent": 0,
"guid": "/?p=277",
"menu_order": 2,
"post_type": "nav_menu_item",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": 277,
"menu_item_parent": "0",
"object_id": "272",
"object": "page",
"type": "post_type",
"type_label": "Page",
"url": "/",
"title": "About",
"target": "",
"attr_title": "",
"description": "……………………………………………",
"classes": [
""
],
"xfn": "",
"menu_item_parent_object_id": "blank",
"menu_item_parent_type": "blank",
"menu_item_parent_title": "blank",
"menu_item_parent_url": "blank",
"menu_item_parent_id": "blank",
"menu_item_parent_post_name": "blank"
},
I'm attempting to read a JSON file that was was successfully exported using wp_send_json_success
however I'm unable to loop through the array in the file since it seems that I can't access $menu->terms
. What am I overlooking?
$json_content = file_get_contents( ABSPATH . '/menu.json', true );
$menus = json_decode( $json_content );
if ( ! empty( $menus ) ) {
foreach ( $menus as $menu ) {
// Get the internal menu by slug.
$internal_menu = get_term_by( 'slug', 'primary', 'nav_menu' );
// Check if a menu with the specified name already exists.
$menu_exists = wp_get_nav_menu_object( 'Primary' );
if ( $menu_exists ) {
// If the menu exists, delete it.
wp_delete_nav_menu( 'Primary' );
}
// Create a new menu with the same name.
$menu_id = wp_create_nav_menu( 'Primary' );
// Build arguments for new menu items.
$args = array();
$index = 0;
if ( ! empty( $menu->terms ) ) {
foreach ( $menu->terms as $menu_item ) {
// Skip if the post status is 'draft'.
if ( 'draft' === $menu_item->post_status ) {
continue;
}
// Process menu item based on its type.
$args_menu_item = process_menu_item( $menu_item );
// Update nav menu item and get the updated ID.
$nav_menu_item_update = wp_update_nav_menu_item( $menu_id, 0, $args_menu_item );
// Update the object ID in the arguments.
$args_menu_item['menu-item-object-id'] = $nav_menu_item_update;
$args[ $index++ ] = $args_menu_item;
}
}
// Update parent ID for menu items.
update_parent_id( $menu_id, $args );
}
}
}
Here's an excerpt from the JSON file that $menu
spits out
{
"success": true,
"data": [
{
"term_id": 4,
"name": "Primary",
"slug": "primary",
"term_group": 0,
"term_taxonomy_id": 4,
"taxonomy": "nav_menu",
"description": "",
"parent": 0,
"count": 31,
"filter": "raw",
"terms": [
{
"ID": 269,
"post_author": "1",
"post_date": "2023-06-27 03:46:59",
"post_date_gmt": "2023-01-01 03:56:36",
"post_content": "…………………………………………",
"post_title": "",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "269",
"to_ping": "",
"pinged": "",
"post_modified": "2023-06-27 03:46:59",
"post_modified_gmt": "2023-06-27 03:46:59",
"post_content_filtered": "",
"post_parent": 0,
"guid": "https://www.example.com/?p=269",
"menu_order": 1,
"post_type": "nav_menu_item",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": 269,
"menu_item_parent": "0",
"object_id": "2",
"object": "page",
"type": "post_type",
"type_label": "Front Page",
"url": "https://www.example.com/",
"title": "Home",
"target": "",
"attr_title": "",
"description": "……………………………………………",
"classes": [
""
],
"xfn": "",
"menu_item_parent_object_id": "blank",
"menu_item_parent_type": "blank",
"menu_item_parent_title": "blank",
"menu_item_parent_url": "blank",
"menu_item_parent_id": "blank",
"menu_item_parent_post_name": "blank"
},
{
"ID": 277,
"post_author": "1",
"post_date": "2023-06-27 03:46:59",
"post_date_gmt": "2023-01-01 04:01:38",
"post_content": "…………………………………………",
"post_title": "About",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "277",
"to_ping": "",
"pinged": "",
"post_modified": "2023-06-27 03:46:59",
"post_modified_gmt": "2023-06-27 03:46:59",
"post_content_filtered": "",
"post_parent": 0,
"guid": "https://www.example.com/?p=277",
"menu_order": 2,
"post_type": "nav_menu_item",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": 277,
"menu_item_parent": "0",
"object_id": "272",
"object": "page",
"type": "post_type",
"type_label": "Page",
"url": "https://www.example.com/about/",
"title": "About",
"target": "",
"attr_title": "",
"description": "……………………………………………",
"classes": [
""
],
"xfn": "",
"menu_item_parent_object_id": "blank",
"menu_item_parent_type": "blank",
"menu_item_parent_title": "blank",
"menu_item_parent_url": "blank",
"menu_item_parent_id": "blank",
"menu_item_parent_post_name": "blank"
},
Share
Improve this question
edited Sep 30, 2024 at 6:46
Motivated
asked Sep 29, 2024 at 5:37
MotivatedMotivated
2352 silver badges10 bronze badges
4
|
2 Answers
Reset to default 2If you want to loop the menus from the json, then you need to use the data
attribute where the menus actually are.
Also you may want to use the current iteration $menu
slug in the get_term_by()
call, unless you specifically want to check for existance of "Primary" menu on each iteration. If that is the case, then it might be more efficient to do the checking outside of the loop so it is done only once.
$menus = json_decode( $json_content );
if ( ! empty( $menus->data ) ) {
foreach ( $menus->data as $menu ) {
// code...
}
}
It looks like you're trying to iterate over all the items in the terms
array, but terms is under data
, not directly in menu
, so your code is just missing the intermediate reference to data
, i.e.:
if ( ! empty( $menu->data->terms ) ) {
foreach ( $menu->data->terms as $menu_item ) {
...
本文标签: How do I read a JSON file and access its properties
版权声明:本文标题:How do I read a JSON file and access its properties? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736286787a1927743.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var_dump($menus)
, what does it show you? If it'snull
, then the json could not be decoded or the structure is deeper than the nesting limit. – Antti Koskinen Commented Sep 29, 2024 at 7:08var_dump($menus)
returns the JSON output. – Motivated Commented Sep 29, 2024 at 16:25$menu
just before the loop. – mmm Commented Sep 29, 2024 at 17:44