dongpo5239 2016-08-24 08:50
浏览 9

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

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?

  • 写回答

1条回答 默认 最新

  • dousonghs58612 2016-08-24 09:40
    关注

    Create four functions, one for each if. Call the functions in the order you prefer. So, let's suppose you have these functions:

    imageTwo, headerTwo, subtextTwo, buttonTwo

    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 functions whenever you need them in the order of your preference.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错