我一直试图建立一个前端表单来发布wordpress。我使用的是自定义帖子类型,但未使用自定义分类法,因此效果很好。
我正在使用wp_insert_post函数执行此操作,尝试发布标题,发布内容,让用户在7个特定类别之间进行选择,并让他们选择ou创建最多5个标签。标题和帖子内容工作正常,但是我既不能发布类别,也不能发布标签。
我已经尝试在 post_category 和 category_name 中创建一个具有特定ID和/或子弹的数组,并在表单上插入,但是没有成功。
这是functions.php上的自定义帖子类型:
function custom_post_type_historia() {
register_post_type('historia', array(
'label' => 'Histórias',
'description' => 'Histórias',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'has_archive' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'historia', 'with_front' => true),
'query_var' => true,
'supports' => array('title', 'editor', 'page-attributes','post-formats'),
'taxonomies' => array('post_tag','category'),
'labels' => array (
'name' => 'Histórias',
'singular_name' => 'História',
'menu_name' => 'Histórias',
'add_new' => 'Adicionar Nova',
'add_new_item' => 'Adicionar Novo História',
'edit' => 'Editar',
'edit_item' => 'Editar História',
'new_item' => 'Nova História',
'view' => 'Ver História',
'view_item' => 'Ver História',
'search_items' => 'Procurar Histórias',
'not_found' => 'Nenhuma História Encontrado',
'not_found_in_trash' => 'Nenhuma História Encontrado no Lixo',
'has_archive' => true,
)
));
}
add_action('init', 'custom_post_type_historia');
这是我的php:
<?php
if( isset($_POST['submit']) ) {
global $user_ID;
$insert_post = array(
'post_title' => $_POST['post_title'],
'post_content' => $_POST['post'],
'post_status' => 'pending',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'historia',
// 'category_name' => array("item1", "item2", "item3", "item4", "item5", "item6", "item7")
'post_category' => array(1, 2, 3, 4, 5, 6, 7),
'post_tag' => $_POST['post_tag']
);
wp_insert_post($insert_post);
}
?>
这是表格:
<form action="" method="post">
<table border="1" width="200">
<tr>
<td><label for="post_title">Post Title</label></td>
<td><input name="post_title" type="text" /></td>
</tr>
<tr>
<td><label for="post">Post</label></td>
<td><input name="post" type="text" /></td>
</tr>
<tr>
<td><label for="post_tag">tag</label></td>
<td><input name="post_tag" type="text" /></td>
</tr>
<tr>
<td><label for="post_category">Q</label></td>
<td><input type="checkbox" name="category_name[7]" value="7" id="category" /></td>
</tr>
</table>
<input name="submit" type="submit" value="submit" />
</form>
了解所有帮助