如何使该 PHP 代码成为循环或更动态?

发布于 2024-12-01 00:40:28 字数 2372 浏览 7 评论 0原文

下面的[示例]代码似乎可以更好地循环或使其更加动态。唯一改变的是字段/变量名称中的 _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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓬勃野心 2024-12-08 00:40:28
<?php
for($i=2 ; $i<5 ; $i++ ){
    $titleNum = sprintf("%02d", $i);
    if (get_custom_field('itinerary_title_'.$titleNum)) {
?>
     <div id="itinerary_entry">
         <h2><?php print_custom_field('itinerary_title_'.$titleNum); ?></h2>
         <p><?php print_custom_field('itinerary_content_'.$titleNum); ?></p>
         <p class="accommodation_name"><?php print_custom_field('itinerary_acc_nme_'.$titleNum); ?></p>
         <p class="accommodation_desc"><?php print_custom_field('itinerary_acc_dsc_'.$titleNum); ?></p>
     <?php if (get_custom_field('itinerary_photo_'.$titleNum.'01')){ ?>
         <div><img src="<?php print_custom_field('itinerary_photo_'.$titleNum.'01'); ?>"></div>
     <?php } ?>
     </div> 
     <?php }
     }
     ?>
<?php
for($i=2 ; $i<5 ; $i++ ){
    $titleNum = sprintf("%02d", $i);
    if (get_custom_field('itinerary_title_'.$titleNum)) {
?>
     <div id="itinerary_entry">
         <h2><?php print_custom_field('itinerary_title_'.$titleNum); ?></h2>
         <p><?php print_custom_field('itinerary_content_'.$titleNum); ?></p>
         <p class="accommodation_name"><?php print_custom_field('itinerary_acc_nme_'.$titleNum); ?></p>
         <p class="accommodation_desc"><?php print_custom_field('itinerary_acc_dsc_'.$titleNum); ?></p>
     <?php if (get_custom_field('itinerary_photo_'.$titleNum.'01')){ ?>
         <div><img src="<?php print_custom_field('itinerary_photo_'.$titleNum.'01'); ?>"></div>
     <?php } ?>
     </div> 
     <?php }
     }
     ?>
过气美图社 2024-12-08 00:40:28

您可以用 for 循环替换重复的代码。

<?php
for ( $i = 2; $i <= 4; $i++) { // Modify this range as needed
    $dynamic_value = ($i < 10) ? "0$i" : $i; // Prefix the value with a '0' when necessary

    if ( get_custom_field("itinerary_title_$i")):?> 
        <!-- HTML and PHP goes here. Replace each hard-coded number with '$i' -->
    <?php
}
?>

You can replace your duplicate code with a for loop.

<?php
for ( $i = 2; $i <= 4; $i++) { // Modify this range as needed
    $dynamic_value = ($i < 10) ? "0$i" : $i; // Prefix the value with a '0' when necessary

    if ( get_custom_field("itinerary_title_$i")):?> 
        <!-- HTML and PHP goes here. Replace each hard-coded number with '$i' -->
    <?php
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文