全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 566|回复: 7

怎么修改wp的自定义分类 发布文章

[复制链接]
发表于 2023-9-11 14:05:34 | 显示全部楼层 |阅读模式
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2023-9-11 14:10:21 | 显示全部楼层
'categories': [1, 2]    换你要的id
 楼主| 发表于 2023-9-11 14:16:41 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2023-9-11 14:23:02 | 显示全部楼层
本帖最后由 usbport 于 2023-9-11 14:27 编辑

post_type不是文章类型吗?taxonomy才是分类

谷歌一搜一大把,https://stackoverflow.com/questions/48270251/wp-rest-api-to-create-a-post-of-a-custom-post-type
发表于 2023-9-11 14:24:47 | 显示全部楼层
直接用插件就完了。没必要自己写这个
发表于 2023-9-11 14:25:26 | 显示全部楼层
'slug': 'custom-category'
 楼主| 发表于 2023-9-11 14:39:19 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2023-9-11 14:47:11 | 显示全部楼层
  1. def make_post(the_title, the_text, your_user, your_password, your_site, wordpress_category):
  2.     import requests
  3.     import base64
  4.    
  5.     your_credentials = your_user + ":" + your_password
  6.     your_token = base64.b64encode(your_credentials.encode())
  7.     your_header = {'Authorization': 'Basic ' + your_token.decode('utf-8')}

  8.     api_url = your_site + '/wp-json/wp/v2/posts'
  9.    
  10.     if not wordpress_category == "":
  11.         # Get the category ID for 'news'
  12.         category_id = get_category_id('news', your_site, your_header)
  13.         
  14.         data = {
  15.             'title': the_title,
  16.             'status': 'publish',
  17.             'content': the_text,
  18.             'categories': [category_id],
  19.         }
  20.     else:
  21.         data = {
  22.             'title': the_title,
  23.             'status': 'publish',
  24.             'content': the_text,
  25.         }
  26.            
  27.     response = requests.post(api_url, headers=your_header, json=data)
  28.     return response.json()

  29. def get_category_id(category_name, your_site, your_header):
  30.     api_url = your_site + '/wp-json/wp/v2/categories'
  31.     response = requests.get(api_url, headers=your_header)
  32.    
  33.     categories = response.json()
  34.     for category in categories:
  35.         if category['news'].lower() == category_name.lower():
  36.             return category['id']
  37.    
  38.     return None
复制代码


引入了一个新函数get_category_id来根据类别名称检索类别 ID
该函数向 WordPress API 的/wp-json/wp/v2/categories端点发出 GET 请求以获取类别列表,
然后按名称搜索所需的类别。如果找到,则返回类别 ID
现在可以将类别名称“news”传递给该make_post函数,它将将该帖子分配给“news”类别
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2026-3-25 07:48 , Processed in 0.064044 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表