Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过PHP代码向Wordpress中的帖子添加类别?_Php_Mysql_Wordpress_Post - Fatal编程技术网

如何通过PHP代码向Wordpress中的帖子添加类别?

如何通过PHP代码向Wordpress中的帖子添加类别?,php,mysql,wordpress,post,Php,Mysql,Wordpress,Post,我不能让它工作。我有一个页面,用户可以提交他们的文章。在那里,理想情况下,我想显示所有类别,让他们通过下拉菜单选择类别来分配帖子。我无法管理这一点,因为我是一个PHP新手,所以我决定一步一步地开始,首先添加一个空的输入字段,用户可以在其中添加一个随机文本,该文本将成为该帖子的类别 为此,我做了以下工作: if(isset($_POST['entry']) AND !$_POST['entry'] == ""):

我不能让它工作。我有一个页面,用户可以提交他们的文章。在那里,理想情况下,我想显示所有类别,让他们通过下拉菜单选择类别来分配帖子。我无法管理这一点,因为我是一个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>

在这里,如果特定类别不存在,则该帖子甚至不会添加到Wordpress中!没有错误,也没有帖子…< /P> < P> <强>请考虑使用<代码> TaskIpPosith PARAM来添加类别。< /强>

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

请不要允许所有用户创建类别。例如,检查是否是管理员

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

<强>请考虑使用<代码> TaskIpPosith PARAM添加类别。< /强>

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

请不要允许所有用户创建类别。例如,检查是否是管理员

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

这在一定程度上是可行的:你不能像这样添加一个类别不存在的帖子。如果没有,我们有没有办法创建一个类别?这在一定程度上是可行的:你不能像这样添加一个类别不存在的帖子。如果没有,我们有没有办法创建一个类别?