0

我正在构建一个 WordPress 主题,人们可以在其中使用wp_insert_post. 下面的代码添加了帖子标题,但没有添加用户指定的类别。相反,它会将其放入uncategorized.

提交时如何将新类别添加到帖子中?

if(isset($_POST['new_post']) == '1') {
    $post_title = $_POST['post_title'];
    $new_cat_ID = $_POST['category'];

    //Checking if category already there
    $cat_ID = get_cat_ID( $_POST['newcat'] );

    //If not create new category
    if($cat_ID == 0) {
       $cat_name = array('cat_name' => $_POST['newcat']);
       wp_insert_category($cat_name);
    }

    //Get ID of newly created category
    $new_cat_ID = get_cat_ID($_POST['newcat']);

    // Create post object
    $new_post = array(
        'ID' => '',
        'post_title' => $post_title,
        'post_status' => 'publish',
        'post_author' => $user->ID,
        'tax_input' => array( 'category' => $new_cat_ID )
    );

    // Insert the post into the database
    $post_id = wp_insert_post($new_post);

    // This will redirect you to the newly created post
    $post = get_post($post_id);
    wp_redirect( home_url() ); 
    exit;
}

这是表单的 HTML:

<form style="" action="" method="post" id="foo">
<input type="hidden" name="new_post" value="1"/>
<input type="text" name="post_title" value="title" id="input-title"/>
<input type="text" name="category" value="apples" id="category" />
<input type="submit" value="Login">
</form>
4

1 回答 1

1

输入category应该是:

<input type="text" name="newcategory" value="apples" id="category" />

在我的测试中,wp_insert_category没有wp_insert_term用,必须使用(根据这个论坛帖子)。

wp_redirect没有采取你所做的事情。该部分//This will redirect you to the newly created post是完全错误的。

以下是一个添加了安全层的工作示例wp_nonce_field,但您必须添加 用户输入数据验证
另外,我在登录时进行测试,所以它可以工作。你的代码没有照顾到$user->IDget_current_user为了得到这个正确的研究。

<?php
if ( isset( $_POST['noncename_so_17539370'] ) && wp_verify_nonce( $_POST['noncename_so_17539370'], 'nonce_so_17539370' ) )
{
    if( isset($_POST['new_post']) == '1' ) {

        //Checking if category already there
        $cat_ID = get_cat_ID( $_POST['newcat'] );

        //If not create new category
        if( !$cat_ID ) {
            $arg = array( 'description' => "my description", 'parent' => 0 );
            $cat_ID = wp_insert_term($_POST['newcat'], "category", $arg);
        }

        // Create post object
        $new_post = array(
            'ID' => '',
            'post_title' => $_POST['post_title'],
            'post_status' => 'publish',
            //'post_author' => $user->ID,
            'tax_input' => array( 'category' => $cat_ID )
        );

        // Insert the post into the database
        $post_id = wp_insert_post($new_post);

        // This will redirect you to the newly created post
        $post = ;
        wp_redirect( get_permalink( $post_id ) ); 
        exit;
    }
} else {
    echo 'ERROR';
}
?>
    <form style="" action="" method="post" id="foo">
    <?php wp_nonce_field( 'nonce_so_17539370', 'noncename_so_17539370' ); ?>
    <input type="hidden" name="new_post" value="1"/>
    <input type="text" name="post_title" value="title" id="input-title"/>
    <input type="text" name="newcat" value="apples" id="category" />
    <input type="submit" value="Login">
    </form>
于 2013-07-09T05:25:50.613 回答