用于将帖子添加到自定义帖子类型的表单

时间:2011-03-06 作者:Jared

基本上,我想在我的博客上显示一个表单(在某个页面上),允许任何人填写表单,并以自定义的帖子类型创建帖子。

我以前看过一次答案,但现在找不到了。

2 个回复
最合适的回答,由SO网友:Bainternet 整理而成

从前端发布是一个显示表单并对其进行处理的问题:

形式:

<!-- New Post Form -->
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="">

<!-- post name -->
<p><label for="title">Title</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title" />
</p>

<!-- post Category -->
<p><label for="Category">Category:</label><br />
<p><?php wp_dropdown_categories( \'show_option_none=Category&tab_index=4&taxonomy=category\' ); ?></p>


<!-- post Content -->
<p><label for="description">Content</label><br />
<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
</p>

<!-- post tags -->
<p><label for="post_tags">Tags:</label>
<input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>

<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( \'new-post\' ); ?>
</form>
</div>
处理过程:

if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "new_post") {

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

    // Add the content of the form to $post as an array
    $new_post = array(
        \'post_title\'    => $title,
        \'post_content\'  => $description,
        \'post_category\' => array($_POST[\'cat\']),  // Usable for custom taxonomies too
        \'tags_input\'    => array($tags),
        \'post_status\'   => \'publish\',           // Choose: publish, preview, future, draft, etc.
        \'post_type\' => \'post_type_name\'  //\'post\',page\' or use a custom post type if you want to
    );
    //save the new post
    $pid = wp_insert_post($new_post); 
    //insert taxonomies
}

SO网友:Aurovrata

使用插件来实现这一点可能更简单。

您也可以使用Contact Form 7 插件以及Post My CF7 Form extension 插件,允许您将任何自定义表单保存到自定义帖子,包括作为特色附件的图像、自定义元字段和作为分类法的选择/复选框/单选输入。

Post My CF7表单插件具有丰富的功能,可用于进一步定制和调整表单的保存方式。有一个详细的documentation section

结束

相关推荐