【问题标题】:Add a post to custom post types category in wordpress from the front end (WordPress)从前端(WordPress)将帖子添加到wordpress中的自定义帖子类型类别
【发布时间】: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'=&gt;$cat, 更改为 'post_category'=&gt;array($cat),,因为它看起来很像标签,它需要一个数组而不是单个值。还请附上$cat 和$tags 的打印件。
  • 感谢您的建议。我最初确实有'post_category' => array($cat)。但是请注意,选择框中只有一个值,因此不需要数组。 $tags = $_POST['post_tags']; $cat = $_POST['类别'];回声$标签;回声$猫;将回显:36(id),tag1

标签: php wordpress custom-post-type categories


【解决方案1】:

只需移动线条

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

把它放在线的下面

$pid = wp_insert_post($new_post);

您正在尝试使用尚不可用的$pid。

【讨论】:

  • 好主意,但没有乐趣。我现在认为这一定与类别设置有关。我注意到有时当我设置一个新的自定义帖子类型时,该帖子类型的类别要么与标准帖子类型的类别相同,要么它们是特定于帖子类型的类别。不确定是什么定义了什么?
【解决方案2】:

玩了一会找到答案:

特别注意名称的更改是唯一的(不是类别而是报告猫,在这种情况下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

【讨论】:

    猜你喜欢
    • 2011-11-19
    • 2021-12-25
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多