You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用WordPressPCL(C#)创建指定分类WordPress文章遇类型错误求助

解决WordPressPCL添加文章分类时的整数类型错误

问题出在你对Post类的Categories属性的使用上——和Title、Content不同,Categories并不需要用Categories包装类初始化,它直接接受整数数组(或整数列表)。

修正后的代码

private async Task mysite()
{
    int[] genres = { 5 };
    var client = new WordPressClient("https://mysite/wp-json/");
    client.Auth.UseBasicAuth("user", "password");
    var post = new Post()
    {
        Title = new Title(textBox1.Text),
        Content = new Content(textBox2.Text),
        Categories = genres // 直接赋值整数数组即可
    };
    await client.Posts.CreateAsync(post);
}

原因说明

WordPressPCL的Post类设计中,Title和Content需要对应包装类(因为WordPress REST API返回的这两个字段是包含rendered属性的对象),但Categories字段直接对应分类ID的数组,不需要额外包装。你之前用new Categories(genres)创建了包装对象,而API期望的是纯整数数组,所以会抛出类型不匹配的错误。

内容的提问来源于stack exchange,提问作者Zeeshanali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 19:25:24