【问题标题】:How to add a category to a post in Wordpress via PHP code?如何通过 PHP 代码将类别添加到 Wordpress 中的帖子?
【发布时间】:2019-05-02 21:07:57
【问题描述】:

我无法让它工作。我有一个页面,用户可以在其中提交他们的帖子。在那里,理想情况下,我想显示所有类别,并让他们通过下拉列表选择类别来分配帖子。由于我是 PHP 新手,所以我无法管理这个问题,所以我决定一步一步地开始,首先添加一个空输入字段,用户可以在其中添加一个随机文本,该文本将成为该帖子的类别。

为此,我做了以下工作:

                   if(isset($_POST['entry']) AND !$_POST['entry'] == ""):
                        $my_post = array();
                        $my_post['post_title'] = $_POST['title'];
                        $my_post['post_content'] = $_POST['entry'];
                        $my_post['post_status'] = 'publish';

                        $cat_name = sanitize_text_field($_POST['newcat']);
                        $my_post ['cat_name'] = $cat_name; 

                        $my_post['post_author'] = get_current_user_id();;

                        // add post to database
                        $id = wp_insert_post( $my_post );
                        endif;

因此,如您所见,我正在尝试通过将类别链接到 $my_post 和“newcat”输入字段来添加类别。

还有形式:

<form action="" method="post" role="form">

<label for="newcat">Category Name</label>
<input type="text" name="newcat" value="" />

</form>

帖子标题和内容正常(从上面的代码中删除),但类别根本不工作。

有人可以帮助我并解释我做错了什么以及如何解决这个问题吗?

编辑 这也不起作用:

  if(isset($_POST['entry']) AND !$_POST['entry'] == ""):
$my_post = array();

$my_post['post_title']   = $_POST['title'];
$my_post['post_content'] = $_POST['entry'];
$my_post['post_status']  = 'publish';

$cat_name             = sanitize_text_field( $_POST['newcat'] );
$my_post ['cat_name'] = $cat_name;

$category_id = get_cat_ID( $_POST['newcat'] );

if ( ! $category_id ) {
    if ( ! is_admin() ) {
        die();
    }
$args = array(
    'description' => "Category description",
    'parent' => 0);
    $category_id = wp_insert_term( $_POST['newcat'], "category", $args );
}

$my_post['post_author'] = get_current_user_id();

$my_post['tax_input'] = array('category' => $category_id);


wp_insert_post($my_post);

在这里,如果该特定类别不存在,甚至不会将帖子添加到 Wordpress!完全没有错误,也没有帖子……

【问题讨论】:

    标签: php mysql wordpress post


    【解决方案1】:

    请考虑使用tax_input 参数来添加类别。

    此外,如果没有,您可以创建一个类别,但要小心权限限制。

    请不要让所有用户都创建一个类别。例如,检查是否是管理员。

    $my_post = array();
    
    $my_post['post_title']   = $_POST['title'];
    $my_post['post_content'] = $_POST['entry'];
    $my_post['post_status']  = 'publish';
    
    $cat_name             = sanitize_text_field( $_POST['newcat'] );
    $my_post ['cat_name'] = $cat_name;
    
    $category_id = get_cat_ID( $_POST['newcat'] );
    
    if ( ! $category_id ) {
        if ( ! is_admin() ) {
            die();
        }
    
        $args        = [ 'description' => "Category description", 'parent' => 0 ];
        $category_id = wp_insert_term( $_POST['newcat'], "category", $args );
    }
    
    $my_post['post_author'] = get_current_user_id();
    
    $my_post['tax_input'] = [ 'category' => $category_id ];
    
    wp_insert_post( $my_post );
    

    【讨论】:

    • 这部分有效:您不能像这样添加类别不存在的帖子。如果没有,我们可以创建一个类别吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多