我正在为WordPress创建一个测验,如果您启用了测验,它会将WordPress帖子内容替换为测验。每个帖子总共有5个问题,要继续,你需要逐一回答每个问题。
我面临的问题是,每当我回答这些问题时,除了问题没有出现在一个页面加载上之外,其他一切都很好。我总是需要刷新才能显示下一个问题。例如,如果我回答第一个问题,它会提交表单并刷新页面,但第二个问题不会出现。当我点击页面刷新时,第二个问题出现了。我可能错过了一些wordpress的东西,不知道是什么atm。有人能帮忙吗?
CODE:
改变\\u内容的函数
/**
*
* @param [type] $content [description]
* @return [type] [description]
*/
function wp_postquiz_content( $content ) {
global $wp_postquiz_status, $questions;;
$user_ID = get_current_user_id();
if ( $wp_postquiz_status == true && is_main_query() ) {
$content = "";
// Get the previously answered questions for all posts.
if ( get_user_meta($user_ID, \'wp_postquiz_user_has_answered\', true) )
$answeredQuestions = get_user_meta($user_ID, \'wp_postquiz_user_has_answered\', true);
// Check how many questions are enabled and store
// them in an array
$q_switch = array();
for ($i=1; $i <= 5 ; $i++) {
$q_switch[] = $questions[\'question-\' . $i . \'\'][\'q_switch\'];
}
// Always get the first question.
$content .= wp_postquiz_display_question(1);
// Get second question if first one is answered
if ( $q_switch[1] == 1 && isset($answeredQuestions) && in_array("post-" . get_the_ID() . "-question-1", $answeredQuestions) )
$content .= wp_postquiz_display_question(2);
// Get third question if second one is answered
if ( $q_switch[2] == 1 && isset($answeredQuestions) && in_array("post-" . get_the_ID() . "-question-2", $answeredQuestions) )
$content .= wp_postquiz_display_question(3);
// Get fourth questino if third one is answered
if ( $q_switch[3] == 1 && isset($answeredQuestions) && in_array("post-" . get_the_ID() . "-question-3", $answeredQuestions) )
$content .= wp_postquiz_display_question(2);
// Get fifth question if fourth one is answered
if ( $q_switch[4] == 1 && isset($answeredQuestions) && in_array("post-" . get_the_ID() . "-question-4", $answeredQuestions) )
$content .= wp_postquiz_display_question(5);
// Store the responses if answered correctly.
if ( isset($_POST[\'question-no\']) ) {
for ($i=1; $i <=5 ; $i++) {
if ( isset($_POST[\'answer-\' . $i . \'\']) ) {
// Get the correct Answer
$q_correct_answer = $questions[\'question-\' . $i . \'\'][\'correct-answer\'];
// If answered correctly store in user meta data.
if ( $_POST[\'answer-\' . $i . \'\'] == $q_correct_answer ) {
wp_postquiz_update_user_metadata( $user_ID, \'wp_postquiz_user_has_answered\', "post-" . get_the_ID() . "-question-" . $i . "");
} else {
$content .= "Wrong Answer";
}
}
}
}
echo $content;
}
}
add_filter( \'the_content\', \'wp_postquiz_content\', 10, 1 );
问题布局功能(wp\\u Postquick\\u display\\u question):
/**
* Get Question from Database for the post
* @param [int] $question_no
* @return [string]
*/
function wp_postquiz_display_question($question_no) {
// Check if Post Quiz is enabled or disabled.
global $questions;
// Get the post id in which quis is being used.
$postid = get_the_ID();
// Question Details
$q_post_content = $questions[\'question-\' . $question_no . \'\'][\'content\'];
$q_question = $questions[\'question-\' . $question_no . \'\'][\'question\'];
$q_option_1 = $questions[\'question-\' . $question_no . \'\'][\'options\'][\'option-1\'];
$q_option_2 = $questions[\'question-\' . $question_no . \'\'][\'options\'][\'option-2\'];
$q_option_3 = $questions[\'question-\' . $question_no . \'\'][\'options\'][\'option-3\'];
$q_option_4 = $questions[\'question-\' . $question_no . \'\'][\'options\'][\'option-4\'];
// HTML to return
$question_html = \'<div class="wp-postquiz-wrapper">
<div class="wp-postquiz content">\' . $q_post_content . \'</div>
<div class="question-wrapper">
<form method="post">
<div class="wp-postquiz question">\' . $q_question . \'</div>
<div class="wp-postquiz question-options">
<select name="answer-\' . $question_no . \'">
<option value="A">\' . $q_option_1 . \'</option>
<option value="B">\' . $q_option_2 . \'</option>
<option value="C">\' . $q_option_3 . \'</option>
<option value="D">\' . $q_option_4 . \'</option>
</select>
</div>
<input type="hidden" name="question-no" value="\' . $question_no . \'">
<input type="hidden" name="post-id" value="\' . $postid . \'">
<input type="hidden" name="action" value="answer-submitted">
<input type="submit" name="submit" class="button primary" value="Answer!">
</form>
</div>
</div>\';
return $question_html;
}