创建带有前端帖子的类别

时间:2012-04-22 作者:Livi17

我正在创建一个buddypress网站,其中一篇帖子将创建一系列相关文章的主干。从那篇主干文章中,相关的帖子分支将向外延伸。

任何在该网站注册的人都可以创建一篇初始主干文章。任何人也可以回复trunk文章。

我需要做的是将所有来自主干文章的分级帖子保持在同一类别中。

因此,当用户创建主干帖子时,他们会在前端帖子表单中添加帖子标题和内容,同时我希望自动创建类别。该类别的标题将与职位标题相同。

那么,对于这篇主干文章中的任何层级帖子,该类别也将应用于它们。

这是我的前端帖子表单:

<?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[\'title\'])) {
        $title =  $_POST[\'title\'];
    } else {
        echo \'Please enter the Title\';
    }
    if (isset ($_POST[\'description\'])) {
        $description = $_POST[\'description\'];
    } else {
        echo \'Please enter some text\';
    }

    $tags = $_POST[\'post_tags\'];

    // ADD THE FORM INPUT TO $new_post ARRAY
    $new_post = array(
    \'post_title\'    =>  $title,
    \'post_content\'  =>  $description,
    \'post_category\' =>  array(5),//array($_POST[\'cat\']),   Usable for custom taxonomies too
    \'tags_input\'    =>  array($tags),
    \'post_status\'   =>  \'publish\',           // Choose: publish, preview, future, draft, etc.
    \'post_type\' =>  \'post\',  //\'post\',page\' or use a custom post type if you want to

    );

    //SAVE THE POST
    $pid = wp_insert_post($new_post);

             //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
    wp_set_post_tags($pid, $_POST[\'post_tags\']);

    //REDIRECT TO THE NEW POST ON SAVE
    $link = get_permalink( $pid );
    wp_redirect( $link );


    //INSERT OUR MEDIA ATTACHMENTS
    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.
        }

    } // END THE IF STATEMENT FOR FILES

} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

//POST THE POST YO
do_action(\'wp_insert_post\', \'wp_insert_post\');

?>

<?php get_header(); ?>

        <div id="container">
            <div id="content" role="main">

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <?php if ( is_front_page() ) { ?>
                        <h2 class="entry-title"><?php the_title(); ?></h2>
                    <?php } else { ?>
                        <h1 class="entry-title"><?php the_title(); ?></h1>
                    <?php } ?>

                    <div class="form-content">
                        <?php the_content(); ?>

        <!-- CREATE TRUNK FORM -->

        <div>
        <form id="new_post" name="new_post" method="post" action="" class="form-stacked storyForm" enctype="multipart/form-data">
            <!-- post name -->
                        <legend>* = indicates a required field</legend>
            <fieldset name="name">
                <label for="title">* Story Title:</label>
                <input type="text" style="width:100%" id="title" value="" tabindex="5" name="title" rel="popover" data-content="Name your story." data-original-title="Story Title"/>
            </fieldset>
                        <script>
                        $(function () {
                          $("#title[rel=popover]")
                            .popover({
                              offset: 10
                            })
                            .click(function(e) {
                              e.preventDefault()
                            })
                        })
                        </script>


                        <!-- post tags -->
            <fieldset class="tags">
                <label for="post_tags">Additional Keywords (comma separated):</label>
                <input type="text" style="width:100%" value="" tabindex="35" name="post_tags" id="post_tags" rel="popover" data-content="This will just help others find your story in searches." data-original-title="Additional Keywords" />
            </fieldset>
                        <script>
                        $(function () {
                          $("#post_tags[rel=popover]")
                            .popover({
                              offset: 10
                            })
                            .click(function(e) {
                              e.preventDefault()
                            })
                        })
                        </script>

            <!-- post Content -->
            <fieldset class="content">
                <label for="description">Write your story here:</label>
                <textarea id="description" tabindex="15" name="description" cols="80" rows="10" rel="popover" data-content="Write as much as you want. Let others finish it." data-original-title="Main Content"></textarea>
            </fieldset>

                        <script>
                        $(function () {
                          $("#description[rel=popover]")
                            .popover({
                              offset: 10
                            })
                            .click(function(e) {
                              e.preventDefault()
                            })
                        })
                        </script>
<br>

            <fieldset class="submit">
                <input class="btn btn-primary pull-right" type="submit" value="Submit" tabindex="40" id="submit" name="submit" />
            </fieldset>

            <input type="hidden" name="action" value="new_post" />
            <?php wp_nonce_field( \'new-post\' ); ?>
                        </form> 
现在我有“\'post_category\' => array(5)“但它需要动态创建一个新的帖子类别。

1 个回复
SO网友:Bainternet

看看wp_insert_term 可用于创建新类别,例如:

wp_insert_term($termName,$taxonomyName);
在你的情况下$termName 应为标题和$taxonomyName 应该是category

结束