Support

Account

Home Forums Front-end Issues My if/else php not working – help!

Solved

My if/else php not working – help!

  • Hello.
    I’ve got a slideshow on a post page that is working nicely.

    However, the client won’t always be able to upload even one image so I want a fallback.

    This is my existing code:

    <!-- slideshow -->
    <?php if( have_rows('news_slideshow') ): ?>
    <!-- data-ratio="820/542" -->
    <div class="the-slideshow slideshow-news">		
    	<div class="fotorama" 
    		data-width="100%" 
    		data-ratio="820/542" 
    		data-minwidth="285" 
    		data-loop="true" 
    		data-swipe="true" 
    		data-click="true"
    		data-nav="false"
    		data-transition="crossfade"
    		data-transitionduration="1500"
    		data-autoplay="4000">
    		<?php while ( have_rows('news_slideshow') ) : the_row(); ?>
    			<?php 
    			$slideLarge = get_sub_field('news_slideshow_image');
    			$size = 'full'; // (thumbnail, medium, large, full or custom size)
    			?>	
    			<?php echo wp_get_attachment_image( $slideLarge, $size );  ?>
    		<?php endwhile; ?>
    	</div>
    </div>
    <?php endif; ?>	
    <!-- end slideshow -->

    I tried to modify it to include an else statement so that if no image were uploaded I could display the contents of another field.

    This is the code I tried (which failed):

    <!-- slideshow -->
    <?php if( have_rows('news_slideshow') ): ?>
    <!-- data-ratio="820/542" -->
    <div class="the-slideshow slideshow-news">		
    	<div class="fotorama" 
    		data-width="100%" 
    		data-ratio="820/542" 
    		data-minwidth="285" 
    		data-loop="true" 
    		data-swipe="true" 
    		data-click="true"
    		data-nav="false"
    		data-transition="crossfade"
    		data-transitionduration="1500"
    		data-autoplay="4000">
    		<?php while ( have_rows('news_slideshow') ) : the_row(); ?>
    			<?php 
    			$slideLarge = get_sub_field('news_slideshow_image');
    			$size = 'full'; // (thumbnail, medium, large, full or custom size)
    			?>	
    			<?php echo wp_get_attachment_image( $slideLarge, $size );  ?>
    		<?php endwhile; ?>
    	</div>
    </div>
    
    <?php else: ?>
    	<?php echo 'Something else' ?>
    <?php endif; ?>
    <!-- end slideshow -->

    Could any one point out where I’m going wrong here?

    Thanks!

  • Just solved it myself! Here’s the answer (might conceivably be of use to someone else):

    <!-- slideshow -->
    <?php if( have_rows('news_slideshow') ): ?>
    <!-- data-ratio="820/542" -->
    <div class="the-slideshow slideshow-news">		
    	<div class="fotorama" 
    		data-width="100%" 
    		data-ratio="820/542" 
    		data-minwidth="285" 
    		data-loop="true" 
    		data-swipe="true" 
    		data-click="true"
    		data-nav="false"
    		data-transition="crossfade"
    		data-transitionduration="1500"
    		data-autoplay="4000">
    		<?php while ( have_rows('news_slideshow') ) : the_row(); ?>
    			<?php if( !empty(get_sub_field('news_slideshow_image')) ) : ?>
    				<?php 
    				$slideLarge = get_sub_field('news_slideshow_image');
    				$size = 'full'; // (thumbnail, medium, large, full or custom size)
    				?>	
    				<?php echo wp_get_attachment_image( $slideLarge, $size );  ?>
    			
    			<?php else: ?>
    				<?php echo 'something else'; ?>
    			<?php endif; ?>
    		<?php endwhile; ?>
    	</div>
    </div>
    <?php endif; ?>
    <!-- end slideshow -->	
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.