如何将一个分类关联到WordPress文章?

22

我正在创建一个功能,用于在WordPress中创建自动化文章。目前,这个功能可以创建WordPress博客文章,但是我无法输入类别。

    public class Post
    {

        public string Title { get; set; }

        public string Description { get; set; }

        public string PostType { get; set; }

        public DateTime DateCreated { get; set; }

        public DateTime DateCreatedGmt { get; set; }

        public List<string> Categories { get; set; }

        public List<string> MtKeywords { get; set; }

        public string MtExcerpt { get; set; }

        public string MtTextMore { get; set; }

        public string MtAllowComments { get; set; }

        public string MtAllowPings { get; set; }

        public string WpSlug { get; set; }

        public string WpPassord { get; set; }

        public string WpAuthorId { get; set; }

        public string WpAuthorDisplayName { get; set; }

        public string PostStatus { get; set; }

        public string WpPostFormat { get; set; }

        public bool Sticky { get; set; }

        public List<CustomFields> CustomFields;

        public Enclosure Enclosure;
    }

我尝试先获取类并仅传递类别名称以避免错误:

        var wordpress  = XmlRpcProxyGen.Create<IWordpress>();

        Category[] categories= wordpress.getCategories(0, username, password);

这个方法是在Post类中定义的,它接收一个参数来构建文章所属的类别。通过这种方式将类别插入到文章中:

        Categories.Add(category.categoryName);

有人可以帮帮我吗?我看了很多次这段代码,但就是找不出错在哪里:(.

Post 的属性在文档中获取:http://codex.wordpress.org/XML-RPC_MetaWeblog_API#metaWeblog.newPost。我正在使用 CookComputing.XmlRpc - http://xml-rpc.net/ - 版本为 2.5.0。

我发现问题在于错误地处理类别(category)。

要发布这篇文章,我们创建的代码如下:

class MetaWeblogClient : XmlRpcClientProtocol
{

    [XmlRpcMissingMapping(MappingAction.Ignore)]

    public struct Post
    {
        public DateTime dateCreated;
        public string description;
        public string title;
        public string[] categories;
        public string permalink;
        public string postid;
        public string userid;
        public string wp_slug;

    }

在下面初始化属性:

    public void newPost(string userid, string password, string description, string title)
    {
        this.Url = "http://#########.wordpress.com/xmlrpc.php";

        Post post = new Post();

        post.categories = new string[1];
        post.categories[0] = "Category Name";
        post.dateCreated = DateTime.Now;
        post.userid = userid;
        post.description = description;
        post.title = title;

        newPost("0", userid, password, post, true);

    }

    [XmlRpcMethod("metaWeblog.newPost")]

    public string newPost(string blogid, string authorId, string password, MetaWeblogClient.Post string description, bool publish)
    {
        //return string postid
        return returnPostId = (string)this.Invoke("newPost", new Object[] { blogid, authorId, password, description, publish });

    }

我的错误在于没有参照初始化数组类别。上面的结构不正确,我将从我的代码中删除它。

感谢您的关注。


2
你已经尝试过使用字符串数组(string[] categories)而不是列表(List<..>)了吗? - vstm
我在发布问题后意识到我处理类别的方式有多么错误。我将发布如何解决这个问题的方法。我为之前没有看到它感到羞愧。你是正确的。感谢你的答案:D - jAbreu
1
嗨 @jAbreu - 你能发布你对问题的解决方案并标记为答案吗?谢谢! - doublesharp
示例代码无效,需要使用正确的类别才能正常工作。 - doublesharp
1个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接