发生的事情是,全局变量$post
没有出现在帖子页面上-因此在下面的条件中,表单正在处理,
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && ! empty($_POST[\'post_id\']) && ! empty($_POST[\'post_title\']) && isset($_POST[\'postcontent\']) ) {
header(\'Location: /\');
$post_id = $post->ID;
$post = array(
\'ID\' => esc_sql($post_id),
\'post_content\' => esc_sql($_POST[\'postcontent\']),
\'post_title\' => esc_sql($_POST[\'post_title\'])
);
wp_update_post($post);
if ( isset($_POST[\'owner_name\']) ) update_post_meta($post_id, \'owner_name\', esc_sql($_POST[\'owner_name\']) );
if ( isset($_POST[\'phone\']) ) update_post_meta($post_id, \'phone\', true );
if ( isset($_POST[\'address\']) ) update_post_meta($post_id, \'address\', true );
if ( isset($_POST[\'zip_code\']) ) update_post_meta($post_id, \'zip_code\', true );
if ( isset($_POST[\'add_coupon\']) ) update_post_meta($post_id, \'add_coupon\', true );
}
$post_id
计算结果为null。
我的解决方案是从$_POST
对象以下是修订后的代码:
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && ! empty($_POST[\'post_id\']) && ! empty($_POST[\'post_title\']) && isset($_POST[\'postcontent\']) ) {
header(\'Location: /\');
$post_id = $post->ID;
$post = array(
\'ID\' => $_POST[\'post_id\'], // use $_POST object instead of global $post
\'post_content\' => esc_sql($_POST[\'postcontent\']),
\'post_title\' => esc_sql($_POST[\'post_title\'])
);
wp_update_post($post);
if ( isset($_POST[\'owner_name\']) ) update_post_meta($_POST[\'post_id\'], \'owner_name\', esc_sql($_POST[\'owner_name\']) );
if ( isset($_POST[\'phone\']) ) update_post_meta($_POST[\'post_id\'], \'phone\', esc_sql($_POST[\'phone\']) );
if ( isset($_POST[\'address\']) ) update_post_meta($_POST[\'post_id\'], \'address\', esc_sql($_POST[\'address\']) );
if ( isset($_POST[\'zip_code\']) ) update_post_meta($_POST[\'post_id\'], \'zip_code\', esc_sql($_POST[\'zip_code\']) );
if ( isset($_POST[\'add_coupon\']) ) update_post_meta($_POST[\'post_id\'], \'add_coupon\', esc_sql($_POST[\'add_coupon\']) );
}
和全文供参考
<?php
/**
* Template Name: Edit Post
*
* This is the default template. It is used when a more specific template can\'t be found to display
* posts. It is unlikely that this template will ever be used, but there may be rare cases.
*/
get_header(); // Loads the header.php template.
do_action( \'before_content\' ); // supreme_before_content
do_action( \'templ_before_container_breadcrumb\' );
?>
<title>Edit your classifieds</title>
<section id="content" class="large-9 small-12 columns">
<?php do_action( \'open_content\' ); ?>
<?php
$user = wp_get_current_user()->data;
$user_id = $user->ID;
$query = new WP_Query(array(
\'post_type\' => \'classified\',
\'post_status\' => \'draft\',
\'author\' => $user_id,
// \'posts_per_page\' => -1
));
echo "<h1>Edit your pending classifieds</h1><br>";
echo "<p>The following posts are your pending classifieds. After you edit a classified, it will be reviewed by our team before publishing.</p>";
?>
<div class="accordion" id="post-listing"><?php
while ($query->have_posts()) {
$query->the_post();
// process form
// if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] ) {
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && ! empty($_POST[\'post_id\']) && ! empty($_POST[\'post_title\']) && isset($_POST[\'postcontent\']) ) {
// header(\'Location: /\');
$post_id = $post->ID;
$post = array(
\'ID\' => $_POST[\'post_id\'],
\'post_content\' => esc_sql($_POST[\'postcontent\']),
\'post_title\' => esc_sql($_POST[\'post_title\'])
);
wp_update_post($post);
// var_dump($post);
// var_dump($post_id);
if ( isset($_POST[\'owner_name\']) ) update_post_meta($_POST[\'post_id\'], \'owner_name\', esc_sql($_POST[\'owner_name\']) );
if ( isset($_POST[\'phone\']) ) update_post_meta($_POST[\'post_id\'], \'phone\', esc_sql($_POST[\'phone\']) );
if ( isset($_POST[\'address\']) ) update_post_meta($_POST[\'post_id\'], \'address\', esc_sql($_POST[\'address\']) );
if ( isset($_POST[\'zip_code\']) ) update_post_meta($_POST[\'post_id\'], \'zip_code\', esc_sql($_POST[\'zip_code\']) );
if ( isset($_POST[\'add_coupon\']) ) update_post_meta($_POST[\'post_id\'], \'add_coupon\', esc_sql($_POST[\'add_coupon\']) );
}
wp_nonce_field( \'update_post_\'. get_the_ID(), \'update_post_nonce\' );
$fields = get_post_meta($post->ID);
$name = $fields["owner_name"][0];
$phone = $fields["phone"][0];
$address = $fields["address"][0];
$zip = $fields["zip_code"][0];
$coupon = $fields["add_coupon"][0];
?>
<!-- <pre><?// php var_dump($post->ID);?></pre> -->
<div class="accordion-navigation step-wrapper">
<a class="step-heading" href="#" onclick="toggle(edit_post_<?php echo $post->ID; ?>);"><span style="float: none;max-width: unset;text-align: left;padding-left: 26px;"><h4 style="margin: 0">Edit <?php echo $post->post_title; ?></h4></span></a>
<div class="step-post content clearfix" id="edit_post_<?php echo $post->ID; ?>" href="#">
<div class="step-post content clearfix current">
<form id="post" class="dropzone form_front_style" method="post" enctype="multipart/form-data">
<input type="hidden" name="post_id" value="<?php the_ID(); ?>" />
<div class="form_row clearfix custom_fileds"><label class="r_lbl" for="post_title">Title</label>
<input class="textfield" type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>" /></div>
<h3>Classified Information</h3>
<div class="form_row clearfix custom_fileds"><label class="r_lbl" for="postcontent">Detail</label>
<span style="color: #333"><?php wp_editor( $post->post_content, \'postcontent\' ); ?></span></div>
<h3>Seller Contact Information</h3>
<div class="form_row clearfix custom_fileds"><label class="r_lbl" for="name">Owner Name</label>
<input class="textfield" type="text" id="name" name="owner_name" value="<?php echo $name; ?>" /></div>
<div class="form_row clearfix custom_fileds"><label class="r_lbl" for="phone">Phone</label>
<input class="textfield" type="text" id="phone" name="phone" value="<?php echo $phone; ?>" /></div>
<div class="form_row clearfix custom_fileds"><label class="r_lbl" for="address">Address</label>
<input class="textfield" type="text" id="address" name="address" value="<?php echo $address; ?>" /></div>
<div class="form_row clearfix custom_fileds"><label class="r_lbl" for="zip_code">Zip</label>
<input class="textfield" type="text" id="zip_code" name="zip_code" value="<?php echo $zip; ?>" /></div>
<h3>Coupons</h3>
<div class="form_row clearfix custom_fileds"><label class="r_lbl" for="add_coupon">Add Coupon</label>
<input class="textfield" <?php echo $coupon ? "disabled style=\'background-color:#f8f8f8;color:#bdbcbc;\'" : ""; ?> type="text" id="add_coupon" name="add_coupon" value="<?php echo $coupon; ?>" /></div>
<input class="textfield" type="submit" id="submit" value="Update" />
</div>
</form>
</div>
</div>
<?php
}
?>
</div>
</section>
<!-- #content -->
<?php do_action( \'after_content\' );
$page_for_posts = get_option( \'page_for_posts\' );
if ( $page_for_posts != \'\' ) {
apply_filters( \'supreme-post-listing-sidebar\',supreme_post_listing_sidebar() );// load the side bar of listing page
} else {
get_sidebar();
}
get_footer();
?>
<script>
// open accordion when anchor link directly above is clicked
function toggle(target) {
var i,
active_elements = document.getElementsByClassName("active"),
clicked = document.getElementById(target[\'id\']);
if (!clicked.classList.contains("active")) {
for (i = 0; i < active_elements.length; i++) {
active_elements[i].classList.remove("active");
console.log("removed");
}
}
clicked.classList.toggle("active");
}
</script>