wordpress - Wordress 前端帖子类别数组
问题描述
我一直在尝试为 wordpress 帖子构建一个前端表单。我使用的是自定义帖子类型,但不是自定义分类法,这些工作正常。
我正在使用 wp_insert_post 函数执行此操作,尝试发布标题、发布内容,让用户在 7 个特定类别之间进行选择,并让他们选择最多创建 5 个标签。标题和帖子内容工作正常,但我不能发布类别和标签。
我已经尝试在post_category和category_name中创建一个数组,其中包含具体的 ID 和/或 slug 并在表单上插入,但没有成功。
这是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>
感谢所有帮助
解决方案
推荐阅读
- angular - 多个组件中的访问服务
- php - 无法从服务器获取页面信息
- java - 如何在 Eclipse 上安装 AST-View 时修复错误
- c# - 如何将视图中的对象绑定到控制器?
- ios - 命令 CompileXIB 失败,退出代码为非零
- laravel - touch(): Utime failed: No such file or directory error Excel::download() in laravel using maatwebsite\Excel
- php - 仅显示从数据库中选择的值
- html - 之间的区别
和 - php - 如何为 Geoip 设置/安装 Geolite2
- java - 如何在 Java Button (Swing/GUI) 点击上运行 python 代码?