WordPressSharp
WordPressSharp copied to clipboard
Post to a Specific Category?
How can I make a blog post to a specific category?
Does any one have a code example I can use?
Thanks - Glenn
I have a working example that was in one of the other posts here. But I need to know how I can get a list of category names/IDs from the client so I can post to the category of my choosing. Can I get a code snippet on doing that, please?
Thanks
Get the category ID:
click “Edit” ,look at the url
http://localhost/wordpress/wp-admin/term.php?taxonomy=category&tag_ID=1&post_type=post&wp_http_referer=%2Fwordpress%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory
Category is "tag_ID=" Back number,such as :1
/// <summary>
/// 发布到指定分类
/// </summary>
/// <param name="post"></param>
/// <param name="category"></param>
/// <param name="localUserInfo"></param>
/// <returns></returns>
internal static string PublishConent(Post post, List<int> category, Wphelper.UserInfo localUserInfo)
{
List<Term> TremsTemp = new List<Term>();
WordPressClient wordPressClient = Wphelper.InitClient(localUserInfo);
foreach (int count in category)
{
try
{
TremsTemp.Add(wordPressClient.GetTerm("category", count));//如果错误出现在这里就代表分类id写错了
}
catch (Exception ex)
{
PrintLog.Log(ex);
}
}
post.Terms = TremsTemp.ToArray();
return PublishConent(post, wordPressClient);
}
public struct UserInfo { private string userName; private string password; private string indexurl;
public string UserName { get => userName; set => userName = value; }
public string Password { get => password; set => password = value; }
public string Indexurl { get => indexurl; set => indexurl = value; }
}