那么你想让人们从前端创建帖子?当然
In this answer here I explain a very basic way in which you can achieve this very thing, front-end posts to a post type of your choice.
下面是一个基本示例,说明您可以在模板文件中包含哪些内容来执行此操作;
(详情请参见原始答案)
<?php
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == "my_post_type") {
//store our post vars into variables for later use
//now would be a good time to run some basic error checking/validation
//to ensure that data for these values have been set
$title = $_POST[\'title\'];
$content = $_POST[\'content\'];
$post_type = \'my_custom_post\';
$custom_field_1 = $_POST[\'custom_1\'];
$custom_field_2 = $_POST[\'custom_2\'];
//the array of arguements to be inserted with wp_insert_post
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $content,
\'post_status\' => \'publish\',
\'post_type\' => $post_type
);
//insert the the post into database by passing $new_post to wp_insert_post
//store our post ID in a variable $pid
$pid = wp_insert_post($new_post);
//we now use $pid (post id) to help add out post meta data
add_post_meta($pid, \'meta_key\', $custom_field_1, true);
add_post_meta($pid, \'meta_key\', $custom_field_2, true);
}
?>
<!-- language: lang-html -->
<form method="post" name="front_end" action="" >
<input type="text" name="title" value="My Post Title" />
<input type="text" name="content" value="My Post Content" />
<input type="text" name="custom_1" value="Custom Field 1 Content" />
<input type="text" name="custom_2" value="Custom Field 2 Content" />
<button type="button">Submit</button>
<input type="hidden" name="action" value="my_post_type" />
</form>
此示例没有任何错误检查、验证和清理。这只是演示如何获取表单数据并将其存储到post类型中。