1

I have the code below that is selecting a random set of questions from Wordpress.

<?php
    $rows = get_field('step_by_step_test');
    $row_count = count($rows);
    $rand_rows = array();
    $questions = get_field('select_number_of_questions');
    for ($i = 0; $i < min($row_count, $questions); $i++) {
        $r = rand(0, $row_count - 1);
        while (array_search($r, $rand_rows) !== false) {
            $r = rand(0, $row_count - 1);
        }
        $rand_rows[] = $r;
        echo $rows[$r]['question'];
    }
?>

I want to incorporate a bit of extra code (below), how can I make sure it's selecting the same random question?

<?php if(get_sub_field('answer_options')): ?>
<?php while(has_sub_field('answer_options')): ?> 
    <?php echo the_sub_field('answer'); ?>
<?php endwhile; ?> 
<?php endif; ?>
4

3 回答 3

3

你为什么不稍微改变你的方法?

<?php 
   $rows = get_field('step_by_step_test');  // Get the test
   $question_count = get_field('select_number_of_questions');  // Get the number of questions
   $rows = shuffle($rows);   // Randomize your questions
   $rows = array_slice($rows, $question_count);   // Now set the array to only contain the number of questions you wanted
   foreach ($rows as $row) {
       echo $row['question'];    // Show the question
       if(get_sub_field('answer_options', $row['id'])) {
              while(has_sub_field('answer_options', $row['id'])) {
                       echo the_sub_field('answer');
              }
       }
   } 
?>

我假设您可以更改“get_sub_field”以包含问题的 ID,因此您可以将 ID 包含在“answer_options”的“where”字段中。这将允许您链接问题。

于 2012-09-30T04:12:02.387 回答
1

我认为你需要的是在一个循环中设置整个事情。按自定义字段查询

或者您可以存储您在上面得到的问题的 ID,然后在下面查询这些特定帖子的答案

于 2012-09-29T23:04:17.647 回答
0

以下是我如何使用 Advanced Custom Fields 插件 + Royal Slider 和上面 TheSwiftExchange 代码的修改版本随机化我的 WordPress 滑块

<div id="full-width-slider" class="royalSlider heroSlider rsMinW">

<?php
   /*
    *  Slider Repeater field shuffled
    *  http://stackoverflow.com/questions/12563116/incorporating-extra-loop-into-random-selection
    */


    $rows = get_field('slider');
//  For Debugging:
//  echo "<pre>";
//  var_dump($rows);
//  echo "</pre>";

    $quotes = get_field('slide_text');  // Get the number of images
    shuffle($rows);   // Randomize your slides

    foreach ($rows as $row) {

        $slideImageID =  $row['slide_image'];
        $slideImage = wp_get_attachment_image_src( $slideImageID, 'full' );
        $slideText = $row['slide_text'];
        ?>

        <div class="rsContent">

               <div class="royalCaption">
                   <div class="royalCaptionInner">

                       <div class="infoBlock">
                            <?php if(!empty($slideText)) {
                                   echo $slideText;
                           }; ?>
                       </div>
                   </div>
               </div>
               <img src="<?php echo $slideImage[0]; ?>" class="" />

        </div><!-- /.rsContent -->
    <?php }  // end foreach ?>

</div><!-- /slider-wrap -->
于 2012-12-27T21:38:13.150 回答