简体   繁体   English

高级自定义字段如果为true则执行此操作

[英]Advanced custom fields if true do this else

I have a tickbox for in my advanced custom fields that when ticked it puts the image above the text, I have it working but I think it can be done better than I have it. 我在我的高级自定义字段中有一个复选框,当勾选它将图像放在文本上方时,我有它工作,但我认为它可以比我拥有的更好。 Here is what I have in my template: 这是我在模板中的内容:

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

So I'm wondering if there is a way of not having to write in the div structures twice...one with the image above the text and one with the image below the text, is there a cleaner way of doing this? 所以我想知道是否有一种方法可以不必在div结构中写两次...一个是文本上方的图像,另一个是文本下方的图像,是否有更简洁的方法来做到这一点?

Create four function s, one for each if . 创建四个function S,为每个if Call the function s in the order you prefer. 按照您喜欢的顺序调用function s。 So, let's suppose you have these function s: 那么,让我们假设你有这些function

imageTwo , headerTwo , subtextTwo , buttonTwo imageTwoheaderTwosubtextTwobuttonTwo

Example: 例:

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
 }
}

and then call the function s whenever you need them in the order of your preference. 然后根据您的喜好按需要调用function s。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM