php - If only 1 field out of 4 is available -


i using below code on wp site.

<?php     $images = array();     $images[] = $profile_user->banner_image_1;     $images[] = $profile_user->banner_image_2;     $images[] = $profile_user->banner_image_3;     $images[] = $profile_user->banner_image_4;        if(!empty($images[0]) || !empty($images[1])|| !empty($images[2])||   !empty($images[3])){ ?>     <?php echo '<div class="slider2">'; ?>      <?php foreach($images $img): ?>         <?php if(!empty($img)): ?>             <div>                 <img src="                 <?php                        $image_id = $img;                     $post_image_data = wp_get_attachment_image_src( $image_id, $size='profile_banner_img' );                     echo $post_image_data[0];                 ?>" />             </div>         <?php endif; ?>     <?php endforeach; ?>      <?php echo '</div>'; ?> <?php } ?> 

what need use "if" or "array" statement or see if "banner_image_1" has been filled user , display different code.

in other words if banner_image_1 returns information 2,3,4 dont need remove

<div class="slider2"> , </div> 

try this:

<?php  $images = array();  for($i=1; $i<=4; $i++) {     if(!empty($profile_user->{"banner_image_$i"})) {         $images[] = $profile_user->{"banner_image_$i"};     } }  $validpics = count($images);   if($validpics > 0) {     if($validpics > 1) echo '<div class="slider2">';      foreach($images $img_id) {          $img_src = wp_get_attachment_image_src( $img_id, $size='profile_banner_img' );          echo '<div><img src=\"' . $img_src[0] . '" /></div>';      }      if($validpics > 1) echo '</div>'; } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -