import pandas as pd import json from datetime import datetime
excel_file = "网址分类目录.xlsx" df = pd.read_excel(excel_file)
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")
menu_items = []
base_url = "https://www.ake.cx/favorites/"
used_post_ids = set()
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
id_mapping = {row['分类ID']: generate_unique_post_id(row['分类ID'] + 500) for _, row in df.iterrows()}
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)
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'")
|