草庐IT

php - Wordpress 仅在 for 循环中回显一次

coder 2024-04-30 原文

所以我尝试只打印一次一些标签,每个循环有四个标签。

所以本质上,我正在尝试遍历并为每个标签打印一次标签。

第二个是每个循环打印四个标签。

为了更好地解释这里有一些图片。

我想要的 - http://i.imgur.com/LPbqd53.png 目前是什么 - http://i.imgur.com/WqNrgBc.png

这是我的代码

  <div class="wpt_test fill_form">

    <div class="answers">
      <div class="content">
        <form
        <?php foreach ($formAttributes as $key => $value):?>
          <?php echo $key ?>="<?php echo htmlspecialchars(is_array($value) ? json_encode($value) : $value) ?>"
        <?php endforeach; ?>>
        <?php if ($isShowContent): ?><div class="content" style="margin:0;"><?php echo $content ?></div><?php endif ?>    
        <?php if ($shortDescription): ?><div class="short-description"><?php echo $wp->autoParagraphise($shortDescription) ?></div><?php endif ?>
        <?php $wp->doAction('wp_testing_template_fill_form_questions_before') ?>                            
          <?php foreach($questions as $q => $question): /* @var $question WpTesting_Model_Question */ ?>
            <?php $wp->doAction('wp_testing_template_fill_form_question_before', $question, $q) ?>
          <div class="question">      
              <div class="title">
              <span class="question-title" style="width:100%;"><?php echo $question->getTitle() ?>
                <?php $wp->doAction('wp_testing_template_fill_form_label_end', array('required' => true)) ?></span>
                <?php if (!$isMultipleAnswers): ?>
                  <input type="hidden" name="<?php echo $answerIdName ?>[<?php echo $answerIndex ?>]" value="" />
                <?php endif ?>
              </div>
              <div class="answer-block">
                <?php foreach($question->buildAnswers() as $a => $answer): /* @var $answer WpTesting_Model_Answer */ ?>
                  <?php $answerId = 'wpt-test-question-' . $question->getId() . '-answer-' . $answer->getId() ?>
                  <div class="question-titles">
                    <div class="question-labels">
                    <label for="<?php echo $answerId ?>">
                        <input type="<?php echo $isMultipleAnswers ? 'checkbox' : 'radio' ?>" id="<?php echo $answerId ?>"
                            data-errormessage="<?php echo $isMultipleAnswers
                                ? __('Please select at least one answer.', 'wp-testing')
                                : __('Please choose only one answer.', 'wp-testing') ?>"
                            <?php if (0 == $a): ?>required="required" aria-required="true"<?php endif ?>
                            name="<?php echo $answerIdName ?>[<?php echo $answerIndex ?>]" value="<?php echo $answer->getId() ?>" />
                        <?php echo $answer->getTitleOnce() ?>
                    </label>     
                    </div>  
                  </div>  
                  <?php if ($isMultipleAnswers) {$answerIndex++;} ?>
                <?php endforeach ?>
              </div>
              <?php $wp->doAction('wp_testing_template_fill_form_question_after', $question, $q) ?>
              <?php if (!$isMultipleAnswers) {$answerIndex++;} ?>
            <?php endforeach ?>
          </div>

          <?php $wp->doAction('wp_testing_template_fill_form_questions_after') ?>

          <?php if($isFinal): ?>
            <p>
              <input type="submit" class="button" value="<?php echo $submitButtonCaption ?>" />
              <?php if($stepsCounter): ?><span class="steps-counter"><?php echo $stepsCounter ?></span><?php endif ?>
            </p>
          <?php else: ?>
            <div class="wpt_warning">
              <h4><?php echo __('Test is under construction', 'wp-testing') ?></h4>
              <p><?php echo __('You can not get any results from it yet.', 'wp-testing') ?></p>
            </div>
          <?php endif ?>
          <?php foreach($hiddens as $name => $value): ?><input type="hidden" name="<?php echo htmlspecialchars($name) ?>" value="<?php echo htmlspecialchars($value) ?>" /><?php endforeach ?>
        </form>
      </div>
    </div>

  </div>

最佳答案

这绝不是复制粘贴解决方案。您的代码中有一些变量,我不知道它们包含什么,但这应该作为解决方案的潜在想法。本质上,所需的结果是一个数据“表”。是使用表格还是 div 来构造 HTML 结构由您决定。您可以使用诸如 bootstrap 或 foundation 或其他 CSS 框架提供的布局。此示例仅使用一个表来提供结构。您需要将答案标签绘制到一个容器中,例如用于在表头中进行迭代的数组。然后,您可以只用输入来呈现每个答案,而不用关心是否在每次迭代时打印它。希望这对您有所帮助!

<?php
/**
 * @var array $answerLabels
 * @var WpTesting_Model_Question[] $questions
 */
?>
<table>
    <thead>
        <tr>
            <td></td>
            <?php foreach ($answerLabels as $label): ?>
                <td><?= $label ?></td>
            <?php endforeach; ?>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($questions as $question): ?>
            <tr>
                <td><?= $question->getTitle() ?></td>
                <?php foreach ($question->buildAnswers() as $index =>     $answer):
                    $answerId = 'wpt-test-question-' . $question->getId() . '-answer-' . $answer->getId(); 
                ?>
                    <td>
                        <label for="<?= $answerId ?>">
                            <input type="<?= $isMultipleAnswers ? 'checkbox' : 'radio' ?>"    
                                   id="<?= $answerId ?>"
                                   data-errormessage="<?= $isMultipleAnswers
                                       ? __('Please select at least one answer.', 'wp-testing')
                                       : __('Please choose only one answer.', 'wp-testing') ?>"
                                <?php if (0 == $index): ?>
                                    required="required"
                                    aria-required="true"
                                <?php endif ?>
                                   name="<?= $answerIdName ?>[<?= $answerIndex ?>]"
                                   value="<?= $answer->getId() ?>"/>
                        </label>
                    </td>
                <?php endforeach; ?>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

关于php - Wordpress 仅在 for 循环中回显一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42114002/

有关php - Wordpress 仅在 for 循环中回显一次的更多相关文章

  1. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  2. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  3. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  4. ruby-on-rails - Rails 中的 NoMethodError::MailersController#preview undefined method `activation_token=' for nil:NilClass - 2

    似乎无法为此找到有效的答案。我正在阅读Rails教程的第10章第10.1.2节,但似乎无法使邮件程序预览正常工作。我发现处理错误的所有答案都与教程的不同部分相关,我假设我犯的错误正盯着我的脸。我已经完成并将教程中的代码复制/粘贴到相关文件中,但到目前为止,我还看不出我输入的内容与教程中的内容有什么区别。到目前为止,建议是在函数定义中添加或删除参数user,但这并没有解决问题。触发错误的url是http://localhost:3000/rails/mailers/user_mailer/account_activation.http://localhost:3000/rails/mai

  5. ruby-on-rails - 如何重命名或移动 Rails 的 README_FOR_APP - 2

    当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?

  6. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  7. ruby-on-rails - Rake 任务仅调用一次时执行两次 - 2

    我写了一个非常简单的rake任务来尝试找到这个问题的根源。namespace:foodotaskbar::environmentdoputs'RUNNING'endend当在控制台中执行rakefoo:bar时,输出为:RUNNINGRUNNING当我执行任何rake任务时会发生这种情况。有没有人遇到过这样的事情?编辑上面的rake任务就是写在那个.rake文件中的所有内容。这是当前正在使用的Rakefile。requireFile.expand_path('../config/application',__FILE__)OurApp::Application.load_tasks这里

  8. ruby - Ruby 中的闭包和 for 循环 - 2

    我是Ruby的新手,有些闭包逻辑让我感到困惑。考虑这段代码:array=[]foriin(1..5)array[5,5,5,5,5]这对我来说很有意义,因为i被绑定(bind)在循环之外,所以每次循环都会捕获相同的变量。使用每个block可以解决这个问题对我来说也很有意义:array=[](1..5).each{|i|array[1,2,3,4,5]...因为现在每次通过时都单独声明i。但现在我迷路了:为什么我不能通过引入一个中间变量来修复它?array=[]foriin1..5j=iarray[5,5,5,5,5]因为j每次循环都是新的,我认为每次循环都会捕获不同的变量。例如,这绝对

  9. ruby - 我怎样才能只写一次 "Text"并同时检查 path_info 是否包含 'A' ? - 2

    -if!request.path_info.include?'A'%{:id=>'A'}"Text"-else"Text"“文本”写了两次。我怎样才能只写一次并同时检查path_info是否包含“A”? 最佳答案 有两种方法可以做到这一点。使用部分,或使用content_forblock:如果“文本”较长,或者是一个重要的子树,您可以将其提取到一个部分。这会使您的代码变干一点。在给出的示例中,这似乎有点矫枉过正。在这种情况下更好的方法是使用content_forblock,如下所示:-if!request.path_info.inc

  10. ruby-on-rails - rails 上的 ruby : radio buttons for collection select - 2

    我有一个集合选择:此方法的单选按钮是什么?谢谢 最佳答案 Rails3中没有这样的助手。在Rails4中,它是collection_radio_buttons. 关于ruby-on-rails-rails上的ruby:radiobuttonsforcollectionselect,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/18525986/

随机推荐