Home › Forums › Front-end Issues › 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 -->
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.