ACF PHP if else layout - banner / slideshow

时间:2016-08-27 02:44:03

标签: php wordpress image if-statement advanced-custom-fields

我是ACF和PHP的新手,我收到语法错误,但我不确定我做错了什么。

<!--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; ?>

或者我需要一个while循环吗?我尝试将它们组合在一起,但我也遇到了一些语法错误。

<!--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;?>

1 个答案:

答案 0 :(得分:0)

在一些朋友的帮助下弄明白了

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