如何在前台插入我的定制帖子时选中选中类别复选框

时间:2019-04-08 作者:sunshine
if ( isset( $_POST[\'save\'] )  ) {

    if(wp_verify_nonce($_REQUEST[\'_nonce\'], \'submit-post\')){

      // $userid = get_current_user_id();

            $atts = array( 
            \'post_content\'   => $_POST[\'content\'], 
            \'post_title\'   => $_POST[\'title\'],
           //\'post_status\'   => \'draft\', /* It coulde be publish*/
            // \'post_excerpt\'          => \'\',
            \'post_category\' =>  array(\'movies\' => $_POST[\'movies\']),
             //\'comment_status\'        => $_POST[\'comments\'],
            \'post_type\'     => \'movies\'
       // \'post_category\' => array($_POST[\'movies\'])


            );

           // $id = wp_insert_post( $postarr, $wp_error );
           $id = wp_insert_post( $atts, $wp_error );

           $fields = [
                    \'movie_name\',
                    \'movie_release_date\',
                   \'movie_producedby\',
                    \'movie_starcast\',
                    \'movie_type\',
                    \'movie_genre\',
                    \'number_of_stars\'
                   // \'movies\'
            ];

            foreach ( $fields as $field ) {
                if ( array_key_exists( $field, $_POST ) ) {
                    update_post_meta( $id, $field, sanitize_text_field( $_POST[$field] ) );
                }
            }
    }
             else{ die("check seccurity");}

  }  //else{ die("check seccurity");}


            ?>     
                <form id="mvshortcode" name="mvshortcode" method = "post" action="" >

                <?php include plugin_dir_path( __FILE__ ) . \'./movieShortcodeForm.php\';?>
                    <!-- <label for="title">Movie Title:</label>
                        <input type="text" name="title">                     
                    -->             
                    <input type="hidden" name="_nonce" value="<?php echo wp_create_nonce(\'submit-post\') ?>">

                  <input type="submit" name="save">



                </form>
            <?php

}
1 个回复
最合适的回答,由SO网友:sunshine 整理而成

  // set Selected Post CATEGORY cheked 
    wp_set_object_terms( $post_id, array((int)$_POST[\'movies_cat\']), \'movies\' );
} else{ die("check seccurity");}  
//下面给出的代码添加到设计页上

<p class="meta-options hcf_field"> <label for="movies">The Categories</label> <?php wp_dropdown_categories( \'taxonomy=movies&name=movies_cat&show_option_all=Select a category\' ); ?> </p>

相关推荐