wp_insert_post with POST data

时间:2012-03-31 作者:Jamie

我在这个网站上找到了一些wp\\u insert\\u post的代码,我让它工作了。我确实在我期望的类别中找到了这篇文章。然后,我更改了代码示例,将表单中的信息包含在页面上,代码失败。

这是我从这个网站上获得的示例代码。

<?php if(isset($_POST[\'new_reservation\']) == \'1\') {
    $post_title = "My Post";
    //$post_category = \'reservations\';

    $my_post = array();
    $my_post[\'post_title\'] = "My Post";
    $my_post[\'post_content\']  = \'This is my post.\';
    $my_post[\'post_status\']   = \'publish\';
    $my_post[\'post_author\']   = 1;
    $my_post[\'post_category\'] = array(9);

     wp_insert_post($my_post);
    //update_post_meta($post_id, \'reservations\', $customer);

} ?>
然后我把事情改成这个,以便从表单中获得帖子的名称

<form action="" method="post" name="reservations" id="reservations" enctype="multipart/form-data"> 

    <table width="474" border="0" cellspacing="0" cellpadding="0" id="reserve">
        <tr>
            <td class="formTitles"><img src="<?php bloginfo(\'template_directory\');?>/images/name.png" alt="name" width="47" height="10" /></td>
            <td class="formTitles"><img src="<?php bloginfo(\'template_directory\');?>/images/date.png" alt="date" width="43" height="10" /></td>
        </tr>
        <tr>
            <td class="formInput"><input type="text" id="name" name="name" /></td>

                <input type="hidden" name="new_reservation" value="1" /><br />
            <td><input type="submit" name="reserveSubmit" id="reserveSubmit" class="button" value="submit"/> <span class="regularText"> or <a class="clearForm" href="#" onclick="reservation.reset();">Clear Form</a> </span></td>
        </tr>

</table>

<?php if(isset($_POST[\'new_reservation\']) == \'1\') {
    $post_title = $_POST[\'name\'];
    $post_category = \'reservations\';

    $my_post = array();
    $my_post[\'post_title\'] = $post_title;
    $my_post[\'post_content\']  = \'This is my post.\';
    $my_post[\'post_status\']   = \'publish\';
    $my_post[\'post_author\']   = 1;
    $my_post[\'post_category\'] = array(9);// Insert the post into the databasewp_insert_post( $my_post );"



     wp_insert_post($my_post);
    //update_post_meta($post_id, \'reservations\', $customer);

} ?>
每当我尝试使用name变量作为帖子的名称时,代码都会失败

1 个回复
最合适的回答,由SO网友:Rutwick Gangurde 整理而成

将“name”改为其他名称。因为名称是WordPress universe中的保留关键字。Check this and look for \'name\' here.

结束