2014-01-10 54 views
1

我目前工作的地方,我需要從前端創建帖子。 這是我的代碼,從wordpress網站的前端創建自定義帖子。添加標籤,當在Wordpress前端創建帖子

<?php 
    $postTitleError = ''; 

    if (isset($_POST['submitted'])) 
    { 
     if(trim($_POST['postTitle']) === '') 
     { 
      $postTitleError = 'Please enter a title.'; 
      $hasError = true; 
     } 

    $post_information = array(
       'post_title' => wp_strip_all_tags($_POST['postTitle']), 
       'post_content' => $_POST['postContent'], 
       'post_type' => 'product', 
       'post_status' => 'publish' 
         ); 
    wp_insert_post($post_information);  
    } 
?> 

這是我的HTML表單:

<form action="" id="primaryPostForm" method="POST"> 
    <!-- Title --> 
    <input class="addTitle" type="text" name="postTitle" id="postTitle" class="required" /> 
    <!-- Description --> 
    <input class="addDescription required" name="postContent" id="postContent" /> 
    <!-- Submit button --> 
    <button type="submit"><?php _e('Add Product', 'framework') ?></button> 
</form> 

我顯示我的網頁上的所有標籤,但我不知道如何將它們分配到崗位。 目前我能夠使用我的代碼創建帖子,但我需要的只是添加標籤到我的文章。

有什麼想法?

回答

2

您是否嘗試過加入以下到$ post_information =陣列():

'tags_input'=>陣列( '標籤,標籤1,標籤2');

所以,你的代碼應該是這樣的:

$post_information = array( 
     'post_title' => wp_strip_all_tags($_POST['postTitle']), 
     'post_content' => $_POST['postContent'], 
     'post_type' => 'product', 
     'post_status' => 'publish', 
     'tags_input' => array('tag,tag1,tag2') 
); 

這裏假設你已經在你的自定義後類型啓用標籤。

參考:

  • http://www.templatemonster.com/help/wordpress-how-to-enable-and-output-post-tags-for-custom-post-types.html
  • http://stackoverflow.com/questions/10434686/wordpress-wp-insert-post-not-inserting-tags
  • http://stackoverflow.com/questions/12267422/update-and-add-tags- post-on-save-post-using-wp-update-post
+0

不錯。謝謝 – WPguy