add post meta front end edit

时间:2011-08-10 作者:user7715

我试图允许用户在我的帖子中添加元值,但它似乎没有在我的前端帖子编辑中添加元值

<!-- edit Post Form -->
<div id="postbox">
<form id="edit_post" name="edit_post" method="post" action="">
</p>
<p><label for="description">Description</label><br />
<textarea id="description" name="description" ><?php echo $post_to_edit->post_content; ?></textarea>
</p>
<!-- wine Rating -->
<fieldset class="data">
          <label for="data">data</label>
          <input type="text" value="" id="data" size="60" tabindex="20" name="data"></textarea>
</fieldset>

<p align="right"><input type="submit" value="Edit" tabindex="6" id="submit" name="submit" /></p>
<input type="hidden" name="action" value="edit_post" />
<input type="hidden" name="pid" value="<?php echo $post_to_edit->ID; ?>" />
<?php wp_nonce_field( \'edit-post\' ); ?>
</form>
</div>
<?php

if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "edit_post" && isset($_POST[\'pid\'])) {
$the_post = get_post($_POST[\'pid\']); 


$the_post = array(); 
$the_post[\'post_content\'] = $_POST[\'description\'];
$the_post[\'data\'] = array($_POST[\'data\']);

add_post_meta($pid, \'rating\', $the_post[\'data\'], true);

  $pid = wp_update_post($the_post); 


  $link = get_permalink( $pid );
  wp_redirect($link);
}
?>
但我似乎无法让它用于前端编辑我很确定我做得不对帮助。。。

1 个回复
SO网友:Bainternet

这段代码应该首先是有条件的,然后是表单输出,因为在设置了头并正在更新后,您不能使用wp\\u重定向,所以请使用update_post_meta 而不是add_post_meta.

尝试:

    if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "edit_post" && isset($_POST[\'pid\'])) {
    $the_post = get_post($_POST[\'pid\']); 


    $the_post = array(); 
    $the_post[\'post_content\'] = $_POST[\'description\'];
    $the_post[\'data\'] = array($_POST[\'data\']);

    $pid = wp_update_post($the_post); 
    update_post_meta($pid, \'rating\', $the_post[\'data\'], true);

    $link = get_permalink( $pid );
    wp_redirect($link);
}

<!-- edit Post Form -->
    <div id="postbox">
        <form id="edit_post" name="edit_post" method="post" action="">
        <p><label for="description">Description</label><br />
            <textarea id="description" name="description" ><?php echo $post_to_edit->post_content; ?></textarea>
        </p>
        <!-- wine Rating -->
        <fieldset class="data">
                  <label for="data">data</label>
                  <input type="text" value="" id="data" size="60" tabindex="20" name="data"><?php echo (!empty($val = get_post_meta($post_to_edit->ID,\'rating\',true))) ? $val : \'\'; ?></textarea>
        </fieldset>

        <p align="right"><input type="submit" value="Edit" tabindex="6" id="submit" name="submit" /></p>
        <input type="hidden" name="action" value="edit_post" />
        <input type="hidden" name="pid" value="<?php echo $post_to_edit->ID; ?>" />
        <?php wp_nonce_field( \'edit-post\' ); ?>
        </form>
    </div>

结束