从前面发布自定义帖子类型

时间:2011-12-14 12:28:55

标签: php wordpress frontend custom-post-type

我想从前端发布自定义帖子类型'问题',但是当我提交表单时,我一直收到404错误。贝娄是表格和表格处理。我做错了什么?

  
<?
/**
 * Questions processing
 */
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {

    // Do some minor form validation to make sure there is content
    if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
    } else {
        echo 'Please add a question';
    }
    if (isset ($_POST['description'])) {
        $description = $_POST['description'];
    } else {
        echo 'Please add a description';
    }

    // Add the content of the form to $post as an array
    $post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'post_status'   => 'publish',           
        'post_type' => 'question'                 
    );
    wp_insert_post($post);  // Pass  the value of $post to WordPress the insert function


} // end IF

// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post'); 

?>




<h1>Add a question:</h1>

<!-- New Question Form -->

<div>

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

<p><label for="title">Question:</label><br />

<input type="text" value="" name="title" />

</p>

<p><label for="description">Details</label><br />

<textarea name="description" cols="50" rows="6"></textarea>

</p>

<p><input type="submit" value="Ask!" name="submit" /></p>

<input type="hidden" name="post_type" value="question" />

<input type="hidden" name="action" value="new_post" />

<?php wp_nonce_field( 'new-post' ); ?>

</form>

</div>

<!--// New Question Form -->

2 个答案:

答案 0 :(得分:0)

你实际上并不需要add_action位,我感觉$ post变量本身会导致问题。

/**
 * Questions processing
 */
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {

    // Do some minor form validation to make sure there is content
    if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
    } else {
        echo 'Please add a question';
    }
    if (isset ($_POST['description'])) {
        $description = $_POST['description'];
    } else {
        echo 'Please add a description';
    }

    // Add the content of the form to $post as an array
    $new_post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'post_status'   => 'publish',           
        'post_type' => 'question'                 
    );
    $id = wp_insert_post($new_post);  // Pass  the value of $post to WordPress the insert function
    //Returns ID of the new post you just created

为了以防万一,还要在表单标记中添加一个URL:

<form name="new_post" method="post" action="<?php the_permalink(); ?>">

答案 1 :(得分:0)

我想通了:

我删除了该行:

<input type="hidden" name="post_type" value="question" />

我认为Wordpress正在以某种方式使用带有此名称的post变量,如果我自己使用它就会出错。