2014-01-07 67 views
0

我試圖完成與JoeBlogs WordPress的包裝領域。如何添加自定義字段上JoeBlogs WordPress的包裝

我的代碼是:

private void postToWordpress(string title, string postContent,string tags, string aioTitle) 
{  
    string link = this.maskedTextBox1.Text; 
    string username = this.maskedTextBox2.Text; 
    string password = this.maskedTextBox3.Text; 

    var wp = new WordPressWrapper(link + "/xmlrpc.php", username, password); 
    var post = new Post(); 

    post.Title = title; 
    post.Body = postContent; 
    post.Tags = tags.Split(','); 

    string[] cf = new CustomField(); //{ ID = "name", Key = "aiosp_title", Value = "All in One SEO Title" }; 
    cf.ID = "name"; 
    cf.Key = "aiosp_title"; 
    cf.Value = "All in One SEO Title"; 

    post.CustomFields[0] = cf; 

    wp.NewPost(post, false); 
} 

的錯誤是在這一行:

post.CustomFields[0] = cf; 

它是:

類型的未處理的異常 'System.NullReferenceException' 發生在JoeBlogsWordpressWrapperTests.exe

附加信息:對象引用未設置爲 對象的實例。

那麼,如何使用JoeBlogs WordPress Wrapper在C#應用程序上正確使用/添加WordPress自定義字段?

回答

2

下面的代碼修復您NullReferenceException也成功保存自定義字段爲在WordPress中Post

private void postToWordpress(string title, string postContent,string tags, string aioTitle) 
{  
    string link = this.maskedTextBox1.Text; 
    string username = this.maskedTextBox2.Text; 
    string password = this.maskedTextBox3.Text; 

    var wp = new WordPressWrapper(link + "/xmlrpc.php", username, password); 
    var post = new Post(); 

    post.Title = title; 
    post.Body = postContent; 
    post.Tags = tags.Split(','); 

    var cfs = new CustomField[] 
     { 
      new CustomField() 
      { 
       // Don't pass in ID. It's auto assigned for new custom fields. 
       // ID = "name", 
       Key = "aiosp_title", 
       Value = "All in One SEO Title" 
      } 
     }; 

    post.CustomFields = cfs; 

    wp.NewPost(post, false); 
} 

你都拿到NullReferenceException錯誤,因爲你正在創建一個string陣列,並試圖爲它分配Post對象,這是CustomFieldCustomField[]數組的CustomFields財產。

此外,爲了節省CustomFields在數據庫中的Post,你應該通過只在CustomFieldstructKeyValue領域,跳過ID場都在一起。原因是Wordpress自動生成ID字段(也是數據庫中的integer/numeric字段)。我認爲那是導致XmlRpc調用失敗的原因,但我們沒有得到任何錯誤。

試試上面的代碼,它應該工作(我有工作在我的本地WAMP的WordPress安裝)。

最後一個注意事項。儘管CustomField的名稱屬性被稱爲Key,但它不必是唯一的,並且不會強制執行唯一性。因此,舉例來說,如果您使用list of citiesPost填充自定義下拉框,則可以將list of cities作爲一組自定義字段,如下所示。

var cfs = new CustomField[] 
     { 
      new CustomField() 
      { 
       Key = "aiosp_title", 
       Value = "All in One SEO Title" 
      } , 
      new CustomField() 
      { 
       Key = "this is another custom field with HTML", 
       Value = "All in One SEO Title <br/> Keyword 1 <br/><p>This is some more text and html</p>" 
      } , 
      new CustomField() 
      { 
       Key = "list_of_cities", 
       Value = "San Francisco" 
      } , 
      new CustomField() 
      { 
       Key = "list_of_cities", 
       Value = "New York" 
      } 
     }; 

這也將得到保存到了後,與相同Key價值和不同的文本在Value字段的值2個自定義字段。

最後但並非最不重要的,你也可以存儲HTML的自定義字段(如上圖所示)。

+0

其實這是執行,在這裏讀到:https://github.com/alexjamesbrown/JoeBlogs/pull/18 – caffeine

+1

正確的。我在做了一系列試驗和錯誤之後更新了我的答案,以找出代碼未能保存自定義字段的原因。請參閱上面的更新回答:) – Shiva

+0

你對這兩種情況有什麼想法:如果我設置Key =「aiosp_title」工作的很好,我可以在發送後發現數據庫中的值,但如果我嘗試插入Key =「_aiosp_title 「它沒有插入數據庫,_有什麼問題,爲什麼不提交給數據庫? – caffeine

相關問題