【问题标题】:How do I add another ACF option to my elseif statement?如何在 elseif 语句中添加另一个 ACF 选项?
【发布时间】:2017-06-05 11:05:31
【问题描述】:

我想为 page.php 添加另一个横幅图像到我的 Wordpress ACF 选项中。我想这样做,这样我就可以为默认的 Wordpress 模板创建一个默认的横幅图像。我不知道该怎么做。

<?php if (is_page_template('template-projects.php')) { ?>

    <?php
     $banimage2 = get_field('banner_img', 'options'); // Array returned by Advanced Custom Fields
     $banimage2Alt = $banimage2['alt']; // Grab, from the array, the 'alt'
     $banimage2Width = $banimage2['width']; // Grab, from the array, the 'width'
     $banimage2Height = $banimage2['height']; // Grab, from the array, the 'height'
     $banimage2ThumbURL = $banimage2['sizes']['large']; //grab from the array, the 'sizes', and from it, the 'thumbnail'
    ?>

    <img class="box-banner" src="<?php echo $banimage2ThumbURL;?>"  alt="<?php echo $banimage2Alt; ?>"  width="<?php echo $banimage2Width; ?>" height="<?php echo $banimage2Height; ?>" />

<?php } elseif (is_singular('projects')) { ?>

    <?php
     $banimage3 = get_field('pd_banner', $post_id); // Array returned by Advanced Custom Fields
     $banimage3Alt = $banimage3['alt']; // Grab, from the array, the 'alt'
     $banimage3Width = $banimage3['width']; // Grab, from the array, the 'width'
     $banimage3Height = $banimage3['height']; // Grab, from the array, the 'height'
     $banimage3ThumbURL = $banimage3['sizes']['large']; //grab from the array, the 'sizes', and from it, the 'thumbnail'
    ?>

    <img class="box-banner" src="<?php echo $banimage3ThumbURL;?>"  alt="<?php echo $banimage3Alt; ?>"  width="<?php echo $banimage3Width; ?>" height="<?php echo $banimage3Height; ?>" />

<?php } else { ?>
    <?php
     $banimage = get_field('banner_img'); // Array returned by Advanced Custom Fields
     $banimageAlt = $banimage['alt']; // Grab, from the array, the 'alt'
     $banimageWidth = $banimage['width']; // Grab, from the array, the 'width'
     $banimageHeight = $banimage['height']; // Grab, from the array, the 'height'
     $banimageThumbURL = $banimage['sizes']['large']; //grab from the array, the 'sizes', and from it, the 'thumbnail'
    ?>
    <?php if(get_field('banner_img')): ?>
    <img class="box-banner" src="<?php echo $banimageThumbURL;?>"  alt="<?php echo $banimageAlt; ?>"  width="<?php echo $banimageWidth; ?>"  height="<?php echo $banimageHeight; ?>" />
    <?php endif; ?>
<?php } ?>

【问题讨论】:

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


    【解决方案1】:

    我不确定我是否完全得到了您想要做的事情,但是要在您的 if else 语句中添加一个新条件,您可以像这样添加它:

    [...]
    
    <?php elseif (is_singular('projects')) : ?>
    
       [...]
    
    <?php elseif ( [YOUR CONDITION] ) : ?>
    
       [DO YOUR THING]
    
    <?php else : ?>
    
       [...]
    
    <?php endif; ?>
    

    【讨论】:

    • 我想我可能需要 我会修改它看看。感谢您的帮助,我会跟进的。
    【解决方案2】:

    我的回答是在语句底部添加一个额外的 ELSE。

    <?php else: ?>
        <img class="box-banner" src="<?php the_field('banner_img', 'options' ); ?>" />
    
    <?php endif; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多