2010-12-01 56 views
3
<?php 

$submitted = $_POST['submit']; 
$post-title= $_POST['post_title']; 
$post-content= $_POST['post_content']; 


$new_post = array(
      'post_title' => '$post-title', 
      'post_content' => '$post-content', 
      'post_status' => 'publish', 
      'post_author' => $user_ID, 

     ); 
if(isset($submitted)){ 
wp_insert_post($new_post); 
} 

?> 

<form method="post" action=" "> 
<input type="text" name="post_title" size="45" id="input-title"/> 

<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea> 


<input type="hidden" name="cat" value="7,100"/> 

<input class="subput round" type="submit" name="submit" value="Post"/> 
</form> 

我測試了它與表格,它工作正常。但因爲某些原因,我似乎無法讓它與表格一起工作。有任何想法嗎?wp_insert_post使用表格

還有什麼應該在表單的行動?

感謝您的幫助。

回答

8

這應該有效。

<?php 
if(isset($_POST['new_post']) == '1') { 
    $post_title = $_POST['post_title']; 
    $post_category = $_POST['cat']; 
    $post_content = $_POST['post_content']; 

    $new_post = array(
      'ID' => '', 
      'post_author' => $user->ID, 
      'post_category' => array($post_category), 
      'post_content' => $post_content, 
      'post_title' => $post_title, 
      'post_status' => 'publish' 
     ); 

    $post_id = wp_insert_post($new_post); 

    // This will redirect you to the newly created post 
    $post = get_post($post_id); 
    wp_redirect($post->guid); 
}  
?>  

<form method="post" action=""> 
    <input type="text" name="post_title" size="45" id="input-title"/> 
    <?php wp_dropdown_categories('orderby=name&hide_empty=0&exclude=1&hierarchical=1'); ?> 
    <textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea> 
    <input type="hidden" name="new_post" value="1"/> 
    <input class="subput round" type="submit" name="submit" value="Post"/> 
</form> 

如果使用名稱new_post和0值輸入然後加職位。表單不需要任何操作,但是您應該將PHP部分保留在標題頂部。

+0

我試過它在index.php,它不起作用。沒有進入數據庫。嗯,非常感謝 – andrewk 2010-12-01 07:10:21

0

離開表單動作空白意味着它會從應用程序的入口點流表單數據到當前的URL地址欄

<form method="post" action=""> 

感謝

1

是的,這是可能的,很容易實現。這裏是你需要閱讀的所有文檔wp_insert_post