如何使该 PHP 代码成为循环或更动态?
下面的[示例]代码似乎可以更好地循环或使其更加动态。唯一改变的是字段/变量名称中的 _xx 和 _xxyy。我该如何改进这一点,这样我就不必编写 20 个 if 语句?:
<?php if (get_custom_field('itinerary_title_02')):?>
<div id="itinerary_entry">
<h2><?php print_custom_field('itinerary_title_02'); ?></h2>
<p><?php print_custom_field('itinerary_content_02'); ?></p>
<p class="accommodation_name"><?php print_custom_field('itinerary_acc_nme_02'); ?></p>
<p class="accommodation_desc"><?php print_custom_field('itinerary_acc_dsc_02'); ?></p>
<?php if (get_custom_field('itinerary_photo_0201')):?>
<div><img src="<?php print_custom_field('itinerary_photo_0201'); ?>"></div>
<?php endif;?>
</div>
<?php endif;?>
<?php if (get_custom_field('itinerary_title_03')):?>
<div id="itinerary_entry">
<h2><?php print_custom_field('itinerary_title_03'); ?></h2>
<p><?php print_custom_field('itinerary_content_03'); ?></p>
<p class="accommodation_name"><?php print_custom_field('itinerary_acc_nme_03'); ?></p>
<p class="accommodation_desc"><?php print_custom_field('itinerary_acc_dsc_03'); ?></p>
<?php if (get_custom_field('itinerary_photo_0301')):?>
<div><img src="<?php print_custom_field('itinerary_photo_0301'); ?>"></div>
<?php endif;?>
</div>
<?php endif;?>
<?php if (get_custom_field('itinerary_title_04')):?>
<div id="itinerary_entry">
<h2><?php print_custom_field('itinerary_title_04'); ?></h2>
<p><?php print_custom_field('itinerary_content_04'); ?></p>
<p class="accommodation_name"><?php print_custom_field('itinerary_acc_nme_04'); ?></p>
<p class="accommodation_desc"><?php print_custom_field('itinerary_acc_dsc_04'); ?></p>
<?php if (get_custom_field('itinerary_photo_0401')):?>
<div><img src="<?php print_custom_field('itinerary_photo_0401'); ?>"></div>
<?php endif;?>
</div>
<?php endif;?>
提前致谢!
The [sample] code below seems like it could be better in a loop or something to make it more dynamic. The only thing that changes is the _xx and _xxyy in the field/variable name. How can I improve this so I don't have to write 20 if statements?:
<?php if (get_custom_field('itinerary_title_02')):?>
<div id="itinerary_entry">
<h2><?php print_custom_field('itinerary_title_02'); ?></h2>
<p><?php print_custom_field('itinerary_content_02'); ?></p>
<p class="accommodation_name"><?php print_custom_field('itinerary_acc_nme_02'); ?></p>
<p class="accommodation_desc"><?php print_custom_field('itinerary_acc_dsc_02'); ?></p>
<?php if (get_custom_field('itinerary_photo_0201')):?>
<div><img src="<?php print_custom_field('itinerary_photo_0201'); ?>"></div>
<?php endif;?>
</div>
<?php endif;?>
<?php if (get_custom_field('itinerary_title_03')):?>
<div id="itinerary_entry">
<h2><?php print_custom_field('itinerary_title_03'); ?></h2>
<p><?php print_custom_field('itinerary_content_03'); ?></p>
<p class="accommodation_name"><?php print_custom_field('itinerary_acc_nme_03'); ?></p>
<p class="accommodation_desc"><?php print_custom_field('itinerary_acc_dsc_03'); ?></p>
<?php if (get_custom_field('itinerary_photo_0301')):?>
<div><img src="<?php print_custom_field('itinerary_photo_0301'); ?>"></div>
<?php endif;?>
</div>
<?php endif;?>
<?php if (get_custom_field('itinerary_title_04')):?>
<div id="itinerary_entry">
<h2><?php print_custom_field('itinerary_title_04'); ?></h2>
<p><?php print_custom_field('itinerary_content_04'); ?></p>
<p class="accommodation_name"><?php print_custom_field('itinerary_acc_nme_04'); ?></p>
<p class="accommodation_desc"><?php print_custom_field('itinerary_acc_dsc_04'); ?></p>
<?php if (get_custom_field('itinerary_photo_0401')):?>
<div><img src="<?php print_custom_field('itinerary_photo_0401'); ?>"></div>
<?php endif;?>
</div>
<?php endif;?>
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以用
for
循环替换重复的代码。You can replace your duplicate code with a
for
loop.