如果它将每个问题存储为“问题”,则将meta类型存储为存储数组。Wordpress会在存储数组之前自动序列化数组。
因此,当您使用get\\u post\\u meta($post\\u id,“question”)时,它将返回一个序列化字符串数组。(我认为,如果有超过1个结果,我很确定它不会为您取消序列化)
然后只需运行foreach循环并完成每个问题
编辑以显示更多详细信息:
假设我有一个JSON对象,并将其发布到PHP中
$myobject = json_decode($_POST[\'myobject\']);
$contents = $myobject->contents;
所以看看你的JSON应该是这样的:
//the whole things an array
array(
//these are your question things
array(\'question\' => \'First Question\',
\'answers\'=> array(\'First Option\', \'Second Option\', \'Third Option\', \'Fourth Option\'),
\'correctAnswer\' => 1
),
array(\'question\' => \'First Question\',
\'answers\'=> array(\'First Option\', \'Second Option\', \'Third Option\', \'Fourth Option\'),
\'correctAnswer\' => 1
),
array(\'question\' => \'First Question\',
\'answers\'=> array(\'First Option\', \'Second Option\', \'Third Option\', \'Fourth Option\'),
\'correctAnswer\' => 1
)
//end of the array
);
现在,您可以将它们中的每一个存储为帖子的元对象
foreach($contents as $question_array) {
add_post_meta($post_id, \'question\', $question_array);
}
然后,稍后(就像在页面上)如果要显示meta,只需调用它并循环遍历它
$questions = get_post_meta($post_id, \'question\', false);//false by default just showing here because we want an array.
foreach($questions as $question){
$question_array = maybe_unserialize($question);
//now we have the question array from earlier and we can do anything like:
echo "Question: <br>" . $question_array[\'question\'];
echo "These are possible answers:<br><ul>";
foreach($question_array[\'answers\'] as $answer) {
echo "<li>" . $answer . "</li>";
}
}
您可以对该循环中的那些数组执行任何操作,它将遍历它们。我从来没有像那样存储过一个完整的对象,但这应该是完全可能的。让我知道进展如何。