WordPressSharp icon indicating copy to clipboard operation
WordPressSharp copied to clipboard

Post to a Specific Category?

Open GlennIM opened this issue 6 years ago • 3 comments

How can I make a blog post to a specific category?

Does any one have a code example I can use?

Thanks - Glenn

GlennIM avatar Jul 07 '18 16:07 GlennIM

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

GlennIM avatar Jul 07 '18 18:07 GlennIM

Get the category ID: tim 20180727114427 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);
    }

Blackcat156 avatar Jul 27 '18 03:07 Blackcat156

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; }
    }

Blackcat156 avatar Jul 27 '18 03:07 Blackcat156