如何使这个PHP代码循环或更动态?

时间:2011-08-24 18:25:21

标签: php wordpress optimization loops

下面的[示例]代码似乎可以在循环中更好或者使其更具动态性。唯一改变的是字段/变量名称中的_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;?>

提前致谢!

2 个答案:

答案 0 :(得分:1)

您可以使用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
}
?>

答案 1 :(得分:1)

<?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 }
     }
     ?>