编辑比创建要难一些,但不是那么难
首先,您只显示指向作者的编辑链接,以便向循环中添加类似的内容:
global $current_user;
get_currentuserinfo();
while (have_posts()) : the_post();
//regular loop stuff
//and check if the post author is the current user
if ($post->post_author = $current_user->ID){
ehco \'<a href="Editpage?qpost_id=\'.$post->ID.\'">Edit</a>\';
}
endwhile;
现在如果你看看这个链接
ehco \'<a href="Editpage?qpost_id=\'.$post->ID.\'">Edit</a>\';
您将看到它正在寻找一个特定的页面,所以创建一个新页面,根据需要调用它,并在循环中更新它。
现在创建一个新的模板页面可以是您页面的精确副本。php并取出循环部分。而是输入此代码以显示编辑表单。
<?php
if (isset($_GET[\'qpost_id\'])){
//check user again
global $current_user;
get_currentuserinfo();
$Qpost = get_post($_GET[\'qpost_id\']);
if ($current_user->ID = $Qpost->post_author){
$html = \'<h1>Edit - \'. $Qpost->post_title .\'<h1><form name="edit_q" id="edit_q" method="POST" action="">
<input type="text" class="question-title-box" value="\' . $Qpost->post_title; . \'" id="question-title-box" name="title" onfocus="if(this.value == \\\'\' . __("Enter Question Title",\'qna-forum\') . \'\\\'){this.value = \\\'\\\';}" onblur="if(this.value == \\\'\\\'){this.value = \\\'\' . __("Enter Question Title",\'qna-forum\') . \'\\\';}" />
<textarea class="question-box" cols="70" rows="20" id="question-box" name="question" onfocus="if(this.value == \\\'\' . __("Enter Your Question Here",\'qna-forum\') . \'\\\'){this.value = \\\'\\\';}" onblur="if(this.value == \\\'\\\'){this.value = \\\'\' .__("Enter Your Question Here",\'qna-forum\') . \'\\\';}">\' . $Qpost->post_content . \'</textarea>\';
$html .= "<div class=\'question-form-bottom\'>".__(\'Category\',\'qna-forum\').":<select name=\'category\' id=\'category\'>";
$categories = get_categories(array(
\'type\' => \'post\',
\'orderby\' => \'count\',
\'order\'=> \'DESC\',
\'hide_empty\'=>0
));
foreach($categories as $cat){
if(get_option(\'q_cat_\' . $cat->term_id) == "TRUE"){
$html .= \'<option value="\' . $cat->term_id . \'">\' . $cat->cat_name . \'</option>\';
}
}
$html .= "</select>";
$html .= "<input type=\'hidden\' value=\'" . wp_create_nonce( \'edit_q_question_form\' ) . "\' name=\'nonce\' />";
$html .="<input type=\'hidden\' name=\'action\' value=\'edit_q_ask_question\' />";
$html .="<input type=\'hidden\' name=\'q_to_update\' value=\'".$qpost->ID."\' />";
$html .="<input type=\'submit\' name=\'submit\' value=\'submit\' /></form>";
echo $html;
}
}
?>
正如你所看到的,大部分内容都来自插件,我只是对其进行了一些编辑,以满足你的需要。
接下来,您需要更新帖子,为此,您可以使用wp_update_post()在刚刚添加的代码上方添加以下代码:
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == "edit_q_ask_question") {
// Do some minor form validation and make sure there is content
$nonce=$_REQUEST[\'nonce\'];
if (! wp_verify_nonce($nonce, \'edit_q_question_form\') ) die(\'Security check\');
if (isset ($_POST[\'title\'])) {
$title = $_POST[\'title\'];
} else {
echo \'Please enter a title\';
}
if (isset ($_POST[\'question\'])) {
$question = $_POST[\'question\'];
} else {
echo \'Please enter some content\';
}
$new_question = array(
\'post_title\' => $title,
\'post_content\' => $question,
\'post_category\' => array($_POST[\'category\'])
\'ID\' => $_POST[\'q_to_update\'];
);
// Update the post into the database
$new_id = wp_update_post( $new_question );
echo \'Qestion updated and you can see it <a href="\'.get_permalink($new_id).\'">here</a>\';
}
我可能会添加一些安全检查
data validation 这并不完美,但这是一个很好的开始。