wordpress导航的批量导入导出

插件介绍

Export Import Menus 是一个用于批量导入和导出 WordPress 导航菜单的插件。它可以帮助用户快速备份和恢复导航菜单,以便在不同的站点之间共享或迁移导航菜单。

插件特点

  • 批量导入导出:插件可以一次性导入或导出多个导航菜单,而无需手动一个一个地操作。
  • 支持自定义字段:插件可以导出包含自定义字段的导航菜单,以便在恢复时保留自定义设置。
  • 支持菜单位置:插件可以导出和导入不同位置的导航菜单,例如主菜单、侧栏菜单等。
  • 支持文件格式:插件支持导出为 XML 文件格式,以便与其他插件或主题兼容。

插件安装

可以通过以下链接在 WordPress 插件库中下载并安装该插件:Export Import Menus。在 WordPress 后台,导航到“插件” -> “添加新插件”,然后在搜索框中输入 “Export Import Menus”,找到该插件后点击“现在安装”,安装完成后点击“激活”即可使用。

生成json

有能力就自己改代码(代码部分网址必须修改),没能力就改 xlsx 模板填写

分类ID 菜单名称 Slug 菜单描述 上级菜单ID 上级菜单名称 上级菜单Slug
一级分类 ID 一级分类名称 一级分类 slug 一级分类描述 空着即可 空着即可 空着即可
二级分类ID 二级分类名称 二级分类 slug 二级分类描述 一级菜单 ID 一级分类名称 一级分类 slug
import pandas as pd
import json
from datetime import datetime

# Read the Excel file
excel_file = "网址分类目录.xlsx"
df = pd.read_excel(excel_file)

# Get current timestamp for post_date and post_modified
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
current_time_gmt = (datetime.now() - pd.Timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S") # Assuming GMT is 8 hours behind CST

# Initialize the output list
menu_items = []

# Base URL for menu items
base_url = "https://www.ake.cx/favorites/"

# Track used post IDs to ensure uniqueness
used_post_ids = set()

# Function to generate a unique post ID
def generate_unique_post_id(base_id):
post_id = base_id
while post_id in used_post_ids:
post_id += 1
used_post_ids.add(post_id)
return post_id

# Create a mapping of classification ID to post ID
id_mapping = {row['分类ID']: generate_unique_post_id(row['分类ID'] + 500) for _, row in df.iterrows()}

# Process each row in the DataFrame
for index, row in df.iterrows():
classification_id = int(row['分类ID'])
parent_id = int(row['上级菜单ID']) if pd.notna(row['上级菜单ID']) else None
post_id = id_mapping[classification_id]
menu_item_parent = id_mapping.get(parent_id, "0") if parent_id else "0"

menu_item = {
"post": {
"ID": post_id,
"post_author": "1",
"post_date": current_time,
"post_date_gmt": current_time_gmt,
"post_content": row['菜单描述Description'],
"post_title": "",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": str(post_id),
"to_ping": "",
"pinged": "",
"post_modified": current_time,
"post_modified_gmt": current_time_gmt,
"post_content_filtered": "",
"post_parent": parent_id if parent_id else 0,
"guid": f"https://www.ake.cx/?p={post_id}",
"menu_order": index + 1,
"post_type": "nav_menu_item",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": post_id,
"menu_item_parent": menu_item_parent,
"object_id": str(classification_id),
"object": "favorites",
"type": "taxonomy",
"type_label": "网址分类",
"url": f"{base_url}{row['Slug']}",
"title": row['菜单名称'],
"target": "",
"attr_title": "",
"description": row['菜单描述Description'],
"classes": [""],
"xfn": ""
},
"post_metas": {
"_menu_item_type": ["taxonomy"],
"_menu_item_menu_item_parent": [menu_item_parent],
"_menu_item_object_id": [str(classification_id)],
"_menu_item_object": ["favorites"],
"_menu_item_target": [""],
"_menu_item_classes": ["a:1:{i:0;s:0:\"\";}"],
"_menu_item_xfn": [""],
"_menu_item_url": [""],
"menu_ico": ["iconfont icon-category"]
}
}
menu_items.append(menu_item)

# Save the output to a JSON file
with open("wp_menus_aigc_config.json", "w", encoding="utf-8") as f:
json.dump(menu_items, f, ensure_ascii=False, indent=2)

print("json 文件已生成'wp_menus_aigc_config.json'")