【问题标题】:How to create a new wordpress category in front end如何在前端创建一个新的 wordpress 类别
【发布时间】:2011-12-19 13:04:27
【问题描述】:

我希望我的用户能够从前端发帖,并将帖子放在他们创建的类别中。除了创建新类别的可能性之外,我的表单中的所有内容都已完成。

有人知道怎么做吗?

谢谢!!

【问题讨论】:

    标签: wordpress categories frontend


    【解决方案1】:

    使用

    <?php wp_create_category( $cat_name, $parent ); ?>
    

    传递变量

    wp_create_category($category);
    

    或,

    挂钩 create_category 操作

    【讨论】:

    • 好的,我确定这是要走的路,但我似乎无法以表格的形式工作。你能给我一个例子来说明如何做到这一点吗?还是这要求太多了?我会理解这是否是该行的结尾,并且我从这里独自一人:)
    • 好吧..看我的另一个答案,这里的评论表格对于代码来说太短了。
    【解决方案2】:

    假设你使用 POST

    //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(
        'post_title' => $headline,
        'post_content' => $body,
        'post_excerpt' => $excerpt,
        'post_date' => $date,
        'post_date_gmt' => $date,
        'post_status' => 'publish',
        'post_author' => 1,
        'post_category' => array($new_cat_ID)
    );
    
    // Insert the post into the database
    wp_insert_post( $new_post );'
    

    你也可以输入这个:$newcat = $_POST['newcat'] and then just replace with $newcat in the code 这可能看起来更好:-)

    请注意,就您而言,wp_insert_category()wp-create_categoy() 具有相同的功能(恕我直言)

    【讨论】:

      【解决方案3】:
      add_action( 'admin_init', function(){
         wp_create_category( 'Custom Category 1' );
         wp_create_category( 'Custom Category 2' );
      });
      

      【讨论】:

        【解决方案4】:

        如果您想从前端添加类别,那么有一种简单的方法:(100% 工作)

        wp_insert_term('Category Name', 'texonomy_type');

        示例:
        wp_insert_term('National', 'category');

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-02-09
          • 1970-01-01
          • 2021-07-04
          • 2015-02-03
          • 1970-01-01
          • 1970-01-01
          • 2021-07-26
          • 2015-08-11
          相关资源
          最近更新 更多