那是因为你在打电话wp_redirect
从代码中获得一些缓冲区输出后。您应该在功能上更改模板的顺序,这意味着首先检查表单是否已提交,然后显示页面,因此请尝试以下操作:
<?php
/*
Template Name: Submit Work
*/
?>
<?php
// Check if the form was submitted
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] )) {
// Do some minor form validation to make sure there is content
if (isset ($_POST[\'title\'])) {
$title = $_POST[\'title\'];
} else {
echo \'Please enter a title\';
}
if (isset ($_POST[\'description\'])) {
$description = htmlentities(trim(stripcslashes($_POST[\'description\'])));
} else {
echo \'Please enter the content\';
}
// Add the content of the form to $post as an array
$type = trim($_POST[\'Type\']);
$post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'post_status\' => \'pending\', // Choose: publish, preview, future, etc.
\'post_type\' => \'work\', // Use a custom post type if you want to
\'tax_input\' => array( $type)
);
$post_id = wp_insert_post($post);
wp_set_post_terms($post_id,$type,\'Type\',true);
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post_id);
// $newupload returns the attachment id of the file that
// was just uploaded. Do whatever you want with that now.
}
}
wp_redirect( \'http://localhost/buddypress/thank-you\' );
exit();
} // end IF
?>
<?php get_header() ?>
<div id="content" class="two_column">
<div class="padder">
<h1 class="page_title">Works<span class="work_logo"></span></h1>
<?php do_action( \'bp_before_blog_page\' ) ?>
<div class="page" id="blog-page" role="main">
<h2 class="posttitle">Submit Work</h2>
<!--SUBMIT POST-->
<div id="postBox">
<form id="new_post" name="new_post" class="post_work" method="post" enctype="multipart/form-data">
<p><label for="title">Title</label><br />
<input type="text" id="title" class="required" value="" tabindex="1" size="20" name="title" />
</p>
<p><label for="description">Description</label><br />
<textarea id="description" type="text" class="required" tabindex="3" name="description" cols="50" rows="6"></textarea>
</p>
<p align="right"><input type="submit" value="Submit" tabindex="6" id="submit" name="submit" /></p>
<p class="post_category"><label for="category">Category</label>
<?php wp_dropdown_categories(\'taxonomy=Type&hide_empty=0&orderby=name&order=asc&name=Type\') ?></p>
<div style="clear:both;"></div>
<p><label for="attachment">Photos: </label><input type="file" id="attachment">
<div id="attachment_list"></div></p>
<input type="hidden" name="post_type" id="post_type" value="domande" />
<input type="hidden" name="action" value="post" />
<?php wp_nonce_field( \'new-post\' ); ?>
</form>
</div>
<script>
var multi_selector = new MultiSelector( document.getElementById( \'attachment_list\' ), 8 );
multi_selector.addElement( document.getElementById( \'attachment\' ) );
</script>
<!--SUBMIT POST END-->
</div><!-- .page -->
<?php do_action( \'bp_after_blog_page\' ) ?>
</div><!-- .padder -->
</div><!-- #content -->
<?php locate_template( array( \'sidebar-work.php\' ), true ) ?>
<?php get_footer(); ?>