【发布时间】:2014-01-24 12:56:28
【问题描述】:
我已经把头发拉了一段时间了……是时候寻求帮助了:
我在 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' => array($cat)。但是请注意,选择框中只有一个值,因此不需要数组。 $tags = $_POST['post_tags']; $cat = $_POST['类别'];回声$标签;回声$猫;将回显:36(id),tag1
标签: php wordpress custom-post-type categories