2014-01-24 100 views
0

我一直在拉我的头发在这一块,现在一段时间...时间寻求帮助:在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. 
    } 
} 

任何指针将不胜感激......甚至只是一个全面的检查!由于

+0

你试过把这行'post_category'=> $ cat'改为'post_category'=> array($ cat)',因为它看起来很像标签,它期望的是一个数组而不是单个值。还请包括'$ cat'和'$ tags'的打印。 –

+0

感谢您的建议。我原本有'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

回答

0

搜了一下玩后回答:

特别要注意名称的变化是唯一的(不范畴,而是报道,猫,CPT在这种情况下,报告中)。

然后线设置后的标签和岗位方面。他们必须在设置好pid之后才能来(正如ziad-saab正确指出的,谢谢)。

最后,代码选择通过下调也需要改变下降的类别。这种方式不需要数组来通过该类别,因为它是通过表单上的str替换函数处理的。如果需要,这也允许多选择选项。

_

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'); 

有一个良好的阅读经过这里,如果你被卡住...我发现了职位之一的答案在底部(感谢罗菲图西) http://voodoopress.com/how-to-post-from-your-front-end-with-no-plugin/#div-comment-716

0

只要招行

wp_set_post_categories($pid, $_POST['category']); 

,并把它的线下

$pid = wp_insert_post($new_post); 

您正在尝试使用$pid当它尚未公布。

+0

不错的想法,但没有喜悦。我现在认为它必须与类别设置有关。我注意到,有时当我设置新的自定义帖子类型时,该帖子类型的类别与标准帖子类型的类别相同,或者它们是帖子类型特定的类别。不确定什么定义了什么? – vincentieo

相关问题