我已经把头发拉过这一段了一段时间了......是时候寻求帮助了:
我在WordPress网站上使用前端表单。表单将发布到自定义帖子类别,附加图像,标签并放入正确的类别。
一切都很好,除了这个类别没有添加到帖子中,我没有IDEA为什么不。我已经多次阅读过codex和其他几个论坛教程,但仍然没有骰子。
我正确地从下拉列表中提取类别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.
}
}
任何指针都会受到高度赞赏......甚至只是一次健全检查!感谢
答案 0 :(得分:0)
只需移动线
即可wp_set_post_categories($pid, $_POST['category'] );
并将其放在
行下面$pid = wp_insert_post($new_post);
您正在尝试使用$pid
,但尚未提供。
答案 1 :(得分:0)
经过一段时间的游戏后找到答案:
特别注意,名称的更改是唯一的(不是类别,而是报告 - 猫,在这种情况下,cpt是报告)。
然后设置发布标签和发布条款的行。他们必须在设置pid之后(正如ziad-saab正确指出的那样,谢谢)。
最后,通过下拉选择类别的代码也需要更改。这样就不需要传递类别的数组,因为它是使用表单上的str替换函数处理的。如果需要,这也允许多选项。
$select_cats = wp_dropdown_categories( array( 'echo' => 0, 'taxonomy' => 'report-cat', 'hide_empty' => 0 ) );
$select_cats = str_replace( "name='cat' id=", "name='cat[]' id=", $select_cats );
echo $select_cats;
_
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'];
$post_cat = $_POST['cat'];
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => $post_cat,
'tags_input' => array($tags),
'post_status' => 'pending', // Choose: publish, preview, future, draft, etc.
'post_type' => 'report', //'post',page' or use a custom post type if you want to
'taxonomy' => 'report-cat'
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
//SET OUR TAGS AND CATEGORIES UP PROPERLY
//wp_set_post_terms( $pid, $_POST['post_tags'], 'report-tag', false );
wp_set_post_tags($pid, $_POST['post_tags']);
wp_set_post_terms( $pid, $_POST['cat'], 'report-cat', false );
// 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.
}
}
//REDIRECT ON SAVE
$link = "/success";
//wp_redirect( $link );
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');
如果你遇到困难,请仔细阅读...我在底部的一个帖子上找到了答案(感谢fitoussi) http://voodoopress.com/how-to-post-from-your-front-end-with-no-plugin/#div-comment-716