使用 While 循环缩短 PHP 代码
Shorten PHP code with While Loop
我正在努力简化下面的代码,但我仍然难以理解它。代码 A 工作正常,它输出图像列表,但代码 B 只显示单个图像
代码A
<?php
$html_form = '<label>
<img src="%1$s" width="%2$s" height="%3$s" alt="%4$s" />
<input type="checkbox" name="%5$s[]" value="%6$s" %7$s/>
</label>';
$html_td = '';
$html = '';
while ($query->have_posts()) {
$query->the_post();
$id = get_the_ID();
$select = '';
$thumb = wp_get_attachment_image_src($id, array(30, 30));
if (is_array($instance['thumbs']) && in_array($id, $instance['thumbs'])) {
$select = 'checked="checked"';
}
$html .= sprintf($html_td);
$html_td = '';
$html_td .= sprintf($html_form, $thumb[0], $thumb[1], $thumb[2], get_the_title(), $this->get_field_name('thumbs'), $id, $select);
}
$html_form = '
<p>
%s
</p>';
printf($html_form, $html);
?>
我的尝试:
代码B
<?php
while ($query->have_posts()) {
$query->the_post();
$id = get_the_ID();
$thumb = wp_get_attachment_image_src($id, array(30, 30));
$thumbs = is_array($instance['thumbs']) ? (bool) in_array($id, $instance['thumbs']) : true;
}
?>
<p>
<label>
<img src="<?php echo $thumb[0] ?>" width="<?php echo $thumb[1] ?>" height="<?php echo $thumb[2] ?>" alt="<?php echo get_the_title() ?>" />
<input class="checkbox" type="checkbox" name="<?php echo $this->get_field_name('thumbs'); ?>" value="<?php echo $id ?>" <?php checked($thumbs); ?>/>
</label>
</p>
如评论中所述,这是因为您只回显了一张图片。
你可以这样做:
<?php while($query->have_posts()):
$query->the_post();
$id = get_the_id();
$thumb = wp_get_attachment_image_src($id, array(30, 30));
$thumbs = is_array($instance['thumbs']) ? (bool) in_array($id, $instance['thumbs']) : true;?>
<p>
<label>
<img src="<?php echo $thumb[0] ?>" width="<?php echo $thumb[1] ?>" height="<?php echo $thumb[2] ?>" alt="<?php echo get_the_title() ?>" />
<input class="checkbox" type="checkbox" name="<?php echo $this->get_field_name('thumbs'); ?>" value="<?php echo $id ?>" <?php checked($thumbs); ?>/>
</label>
</p>
<?php endwhile;
wp_reset_postdata();
在这种情况下,HTML 位于 while 内,每次迭代都会回显图像。另外,请确保您拥有 wp_reset_postdata() 否则您将来可能(或可能不会)遇到错误。
我正在努力简化下面的代码,但我仍然难以理解它。代码 A 工作正常,它输出图像列表,但代码 B 只显示单个图像
代码A
<?php
$html_form = '<label>
<img src="%1$s" width="%2$s" height="%3$s" alt="%4$s" />
<input type="checkbox" name="%5$s[]" value="%6$s" %7$s/>
</label>';
$html_td = '';
$html = '';
while ($query->have_posts()) {
$query->the_post();
$id = get_the_ID();
$select = '';
$thumb = wp_get_attachment_image_src($id, array(30, 30));
if (is_array($instance['thumbs']) && in_array($id, $instance['thumbs'])) {
$select = 'checked="checked"';
}
$html .= sprintf($html_td);
$html_td = '';
$html_td .= sprintf($html_form, $thumb[0], $thumb[1], $thumb[2], get_the_title(), $this->get_field_name('thumbs'), $id, $select);
}
$html_form = '
<p>
%s
</p>';
printf($html_form, $html);
?>
我的尝试: 代码B
<?php
while ($query->have_posts()) {
$query->the_post();
$id = get_the_ID();
$thumb = wp_get_attachment_image_src($id, array(30, 30));
$thumbs = is_array($instance['thumbs']) ? (bool) in_array($id, $instance['thumbs']) : true;
}
?>
<p>
<label>
<img src="<?php echo $thumb[0] ?>" width="<?php echo $thumb[1] ?>" height="<?php echo $thumb[2] ?>" alt="<?php echo get_the_title() ?>" />
<input class="checkbox" type="checkbox" name="<?php echo $this->get_field_name('thumbs'); ?>" value="<?php echo $id ?>" <?php checked($thumbs); ?>/>
</label>
</p>
如评论中所述,这是因为您只回显了一张图片。
你可以这样做:
<?php while($query->have_posts()):
$query->the_post();
$id = get_the_id();
$thumb = wp_get_attachment_image_src($id, array(30, 30));
$thumbs = is_array($instance['thumbs']) ? (bool) in_array($id, $instance['thumbs']) : true;?>
<p>
<label>
<img src="<?php echo $thumb[0] ?>" width="<?php echo $thumb[1] ?>" height="<?php echo $thumb[2] ?>" alt="<?php echo get_the_title() ?>" />
<input class="checkbox" type="checkbox" name="<?php echo $this->get_field_name('thumbs'); ?>" value="<?php echo $id ?>" <?php checked($thumbs); ?>/>
</label>
</p>
<?php endwhile;
wp_reset_postdata();
在这种情况下,HTML 位于 while 内,每次迭代都会回显图像。另外,请确保您拥有 wp_reset_postdata() 否则您将来可能(或可能不会)遇到错误。