lala lala - 3 months ago 30
PHP Question

ACF PHP if else layout - banner/slideshow

I am new to ACF and PHP, I am getting an syntax error, but I am not sure what I am doing wrong.

<!--BANNER STATIC OR SLIDESHOW LAYOUT-->
<?php if(get_row_layout() == 'banner'): // layout: Content ?>
<section class="banner">
<?php
$sbimage = get_field('static_banner'); // Array returned by Advanced Custom Fields
$sbimageAlt = $sbimage['alt']; // Grab, from the array, the 'alt'
$sbimageWidth = $sbimage['width']; // Grab, from the array, the 'width'
$sbimageHeight = $sbimage['height']; // Grab, from the array, the 'height'
$sbimageThumbURL = $sbimage['sizes']['large']; //grab from the array, the 'sizes', and from it, the 'thumbnail'
?>
<?php if(get_field('static_banner')): ?>
<a href="<?php the_field('static_url'); ?>">
<img src="<?php echo $sbimageThumbURL;?>" alt="<?php echo $sbimageAlt; ?>" width="<?php echo $sbimageWidth; ?>" height="<?php echo $sbimageHeight; ?>" /></a>
</section>
<?php else: ?>
<section class="banner">
<?php the_field('slideshow_banner'); ?>
</section>
<?php endif; ?>


OR do I need a while loop? I tried to put that together, but I am also getting a couple of syntax errors on this.

<!--BANNER STATIC OR SLIDESHOW LAYOUT-->
<?php if(get_row_layout() == 'banner_select'): // layout: Content ?>
<?php if( have_rows('banner') ): // check if the flexible content field has rows of data ?>
<?php while ( have_rows('banner') ) : the_row(); // loop through the rows of data?>
<?php if( get_row_layout() == 'static_banner' ):?>
<section class="banner">
<?php
$sbimage = get_field('static_banner'); // Array returned by Advanced Custom Fields
$sbimageAlt = $sbimage['alt']; // Grab, from the array, the 'alt'
$sbimageWidth = $sbimage['width']; // Grab, from the array, the 'width'
$sbimageHeight = $sbimage['height']; // Grab, from the array, the 'height'
$sbimageThumbURL = $sbimage['sizes']['large']; //grab from the array, the 'sizes', and from it, the 'thumbnail'
?>
<?php if(get_field('static_banner')): ?>
<a href="<?php the_field('static_url'); ?>">
<img src="<?php echo $sbimageThumbURL;?>" alt="<?php echo $sbimageAlt; ?>" width="<?php echo $sbimageWidth; ?>" height="<?php echo $sbimageHeight; ?>" /></a>
</section>

<?php elseif( get_row_layout() == 'slidshow_banner' ): ?>
<section class="banner">
<?php the_field('slideshow_banner'); ?>
</section>
<?php endif;?>
<?php endwhile; ?>
<?php else : // no layouts found ?>
<?php endif;?>
<?php endif;?>

Answer

Figured it out with a little help from some friends

<?php if(get_row_layout() == 'banner_select'): // LAYOUT: BANNER STATIC OR SLIDESHOW LAYOUT ?>
    <?php if( get_sub_field('banners') == 'static_banner' ):?>          
        <section class="banner">
            <?php
             $sbimage = get_sub_field('static_banner_img'); // Array returned by Advanced Custom Fields
             $sbimageAlt = $sbimage['alt']; // Grab, from the array, the 'alt'
             $sbimageWidth = $sbimage['width']; // Grab, from the array, the 'width'
             $sbimageHeight = $sbimage['height']; // Grab, from the array, the 'height'
             $sbimageThumbURL = $sbimage['sizes']['large']; //grab from the array, the 'sizes', and from it, the 'thumbnail'
            ?>
            <?php if(get_sub_field('static_banner_img')): ?>
            <a href="<?php the_sub_field('static_url'); ?>">
            <img src="<?php echo $sbimageThumbURL;?>"  alt="<?php echo $sbimageAlt; ?>"  width="<?php echo $sbimageWidth; ?>"  height="<?php echo $sbimageHeight; ?>" /></a>
        </section>
    <?php endif;?>
    <?php elseif( get_sub_field('banners') == 'slideshow_banner' ): ?>
        <section class="banner">
            <?php the_sub_field('slideshow_banner_img'); ?>
        </section>
    <?php endif;?>
<?php endif;?>