我一直在拉我的头发在这一块,现在一段时间...时间寻求帮助:在WordPress后添加到自定义文章类型类别从前端(WordPress的)
我使用一个WordPress网站上的前端表单。表单将发布到自定义帖子类别,附加图像,标签并放入正确的类别。
一切运作良好,除了类别不加入后,我不知道为什么。我已经阅读过很多次的代码和几个其他论坛教程,但仍然没有骰子。
我正确地从下拉
这里(我从简单的呼应价值都知道)拉动类别ID是我。
形式部分:
<label for="category">Type:</label>
<select tabindex="10" class="postform" id="category" name="category">
<option value="35" class="level-0">cat1</option>
<option value="36" class="level-0">cat2</option>
</select>
...
<input type="submit" class="button" name="submit" value="Submit">
的PHP
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['offender_ign'])) {
$title = $_POST['offender_ign'];
} else {
echo 'Please enter the In Game Name (IGN) of the offender';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter some notes about the hate speech';
}
$tags = $_POST['post_tags'];
$cat = $_POST['category'];
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => $cat, //Not in an array as pulled from drop down
'tags_input' => array($tags),
'post_status' => 'pending', // Choose: publish, preview, future, draft, etc.
'post_type' => 'product' //'post',page' or use a custom post type if you want to
);
wp_set_post_categories($pid, $_POST['category']);
//SAVE THE POST
$pid = wp_insert_post($new_post);
//SET OUR TAGS UP PROPERLY
wp_set_post_tags($pid, $_POST['post_tags']);
// Image handling
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
// $newupload returns the attachment id of the file that
// was just uploaded. Do whatever you want with that now.
}
}
任何指针将不胜感激......甚至只是一个全面的检查!由于
你试过把这行'post_category'=> $ cat'改为'post_category'=> array($ cat)',因为它看起来很像标签,它期望的是一个数组而不是单个值。还请包括'$ cat'和'$ tags'的打印。 –
感谢您的建议。我原本有'post_category'\t =>数组($猫)。但看到只有一个值来自选择框,所以不需要数组。 $ tags = $ _POST ['post_tags']; \t \t \t \t \t $ cat = $ _POST ['category']; \t \t \t \t \t echo $ tags; \t \t \t \t \t echo $ cat; Will echo: 36(the id), tag1 – vincentieo