高级自定义字段,如果为真,执行else


Advanced custom fields if true do this else

我有一个复选框在我的高级自定义字段,当勾选它把图像上面的文字,我有它的工作,但我认为它可以做得比我有它。下面是我的模板:

 <?php if( $image_two = get_sub_field('image-two') ): ?>
    <a href="<?php echo $image_link_two; ?>">
 <img src="<?php echo $image_two; ?>" class="col-image"></a>
<?php endif; ?>
<?php if( $header_two = get_sub_field('header-two') ): ?>
  <h2 style="color: <?php echo $text_colour_two; ?>"><?php echo   $header_two; ?></h2>
<?php endif; ?>
<?php if( $subtext_two = get_sub_field('sub-text-two') ): ?>
 <span class="subtext" style="color: <?php echo $text_colour_two; ?>"><?php echo $subtext_two; ?></span>
 <?php endif; ?>
<?php if( $button_text_two = get_sub_field('button-text-two') ): ?>
 <a href="<?php echo $button_link_two; ?>"><button class="btn-text" style="color: <?php echo $button_text_colour_two; ?>; background-color: <?php echo $button_background_two; ?>"><?php echo $button_text_two ?></button></a>
<?php endif; ?>
<?php else: ?>
<?php if( $header_two = get_sub_field('header-two') ): ?>
  <h2 style="color: <?php echo $text_colour_two; ?>"><?php echo $header_two; ?></h2>
<?php endif; ?>
<?php if( $subtext_two = get_sub_field('sub-text-two') ): ?>
   <span class="subtext" style="color: <?php echo $text_colour_two; ?>"><?php echo $subtext_two; ?></span>
<?php endif; ?>
<?php if( $image_two = get_sub_field('image-two') ): ?>
  <a href="<?php echo $image_link_two; ?>"><img src="<?php echo $image_two; ?>" class="col-image"></a>
  <?php endif; ?>
  <?php if( $button_text_two = get_sub_field('button-text-two') ): ?>
     <a href="<?php echo $button_link_two; ?>"><button class="btn-text" style="color: <?php echo $button_text_colour_two; ?>; background-color: <?php echo $button_background_two; ?>"><?php echo $button_text_two ?></button></a>
  <?php endif; ?>
 <?php endif; ?>

所以我想知道是否有一种方法不必在div结构中写入两次…一个是图片在文字上面,一个是图片在文字下面,有没有更简洁的方法?

创建四个function,每个if一个。按您喜欢的顺序呼叫function。那么,假设你有这些function s:

imageTwo, headerTwo, subtextTwo, buttonTwo

的例子:

function imageTwo() {
 <?php if( $image_two = get_sub_field('image-two') ) { ?>
    <a href="<?php echo $image_link_two; ?>">
 <img src="<?php echo $image_two; ?>" class="col-image"></a>
 <php
 }
}

,然后在需要时按您的偏好顺序调用function s。