我的高级自定义字段中有一个复选框,当它勾选它将图像放在文本上方时,我有它工作,但我认为它可以比我拥有的更好。这是我在模板中的内容:
<?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结构中写两次...一个是文本上方的图像,另一个是文本下面的图像,是否有更简洁的方法来执行此操作?
答案 0 :(得分:0)
创建四个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
。