2011-01-28 27 views
1

嗨我正在尋找一種非常簡單的快速(無插件)方式在頁面模板中添加一個表單,該表單允許登錄用戶從WP-Admin外部發布信息。所以基本上這篇文章將在登錄的作者之下。WordPress公共郵局表格

我一直在網上尋找一些教程等左右,但還沒有太多的運氣和許多人似乎要選擇插件等,

誰能幫助表示感謝。

回答

2

您可以使用wp_insert_post()函數。

看看這個。例如,您可以將其粘貼到category.php中,然後訪問類別頁面並進行檢查。但要確保將頂級代碼放在get_header()函數之上。

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

-

<!-- this form shows only if user is logged in --> 
<?php if (is_user_logged_in()) { ?> 

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

<?php } ?> 

更多的信息,你應該看看這裏wp_insert_post with a form

好運