[英]Wordpress echo something in for loop only once
所以我試圖只打印一些標簽一次,每個循環有四個標簽。
所以基本上我試圖循環並為每個標簽打印一次標簽。
在第二個時,它正在為每個循環打印四個標簽。
為了更好地解釋這里有一些圖片。
我想要什么 - 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>
這絕不是復制粘貼解決方案。 您的代碼中有一些變量我不知道它們包含什么,但這應該作為解決方案的潛在想法。 本質上,所需的結果是數據的“表格”。 是否使用 table 或 divs 來構建 HTML 結構取決於您。 您可以使用諸如引導程序或基礎或其他 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 if($i==0): ?>
<?php foreach($question->buildAnswers() as $a => $answer): /* @var $answer WpTesting_Model_Answer */ ?>
<?php $answerId = 'wpt-test-question-' . $question->getId() . '-answer-' . $answer->getId() ?>
<label for="<?php echo $answerId ?>"> <?php echo $answer->getTitleOnce(); ?></label>
<?php if ($isMultipleAnswers) {$answerIndex++;} ?>
<?php endforeach ?>
<?php $i = 1; ?>
<?php endif; ?>
實際上您做錯了,因為您正在使用輸入打印標簽。
你可以先打印靜態標簽,然后你應該打印為
foreach 輸入顯示內聯
您還可以使用 table <tr>
<td>
正確對齊輸入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.