2014-01-07 2 views
0

JoeBlogs WordPress Wrapper로 필드를 완성하려고합니다.JoeBlogs Wordpress Wrapper에 사용자 정의 필드를 추가하는 방법

내 코드는 다음과 같습니다

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에서 발생했습니다.

추가 정보 : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.

그래서 WordPress에서 WordPress의 JoeBlogs WordPress Wrapper를 사용하여 C# 응용 프로그램의 사용자 정의 필드를 올바르게 추가/추가하는 방법은 무엇입니까?

답변

2

다음 코드는 NullReferenceException을 수정하고 사용자 정의 필드를 Wordpress의 Post에 성공적으로 저장합니다. 당신이 string 배열을 만들고 여기에 CustomFieldCustomField[]의 배열입니다 Post 객체의 CustomFields 속성을 할당하려고했기 때문에

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 오류가 발생했습니다.

또한, 데이터베이스의 PostCustomFields을 절약하기 위해, 당신의 CustomFieldstructKeyValue 필드를 통과해야하고 모두 함께 ID 필드를 건너 뜁니다. 이유는 Wordpress을 자동으로 필드를 생성합니다 (또한 integer/데이터베이스의 숫자 필드). 나는 그것이 XmlRpc 호출을 실패하게 만드는 원인이라고 생각하지만, 왜 우리는 어떤 오류를 발견하지 못했습니다.

위의 코드를 시도하면 작동합니다 (로컬 호스트 WAMP Wordpress 설치에서 작동 함).

마지막주의 사항. CustomField의 이름 속성은 Key이지만 고유 할 필요는 없으며 고유성이 적용되지 않습니다. 예를 들어, 에 대해 list of cities의 사용자 지정 드롭 다운 상자를 채우는 경우 다음과 같이 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" 
      } 
     }; 

Value 필드의 값에 같은 Key 값과 다른 텍스트와 2 개 사용자 정의 필드, 게시물에 저장 얻을 것이다.

그리고 위와 같이 사용자 지정 필드에도 HTML을 저장할 수 있습니다.

+0

실제로 구현되었으므로 여기를 읽으십시오. https://github.com/alexjamesbrown/JoeBlogs/pull/18 – caffeine

+1

정확함. 일련의 시행 착오를 거쳐 내 대답을 업데이트하여 코드가 사용자 정의 필드를 저장하지 못하는 이유를 알아 냈습니다.위의 업데이트 된 답변보기 : – Shiva

+0

이 두 가지 경우에 대해 알고 계셨습니까? 내가 Key = "aiosp_title"을 설정하면 훌륭하게 작동하며 게시물을 보낸 후 값을 데이터베이스에서 찾을 수 있지만 Key = "_aiosp_title "데이터베이스에 삽입되지 않았습니다. _에 무엇이 잘못 되었습니까? 왜 데이터베이스에 제출되지 않습니까? – caffeine

관련 문제