php - 如果转发器数组中的第一项,则 ACF 添加类
问题描述
我正在尝试使用 ACF 构建一个站点,该站点在页面加载时具有动画横幅。为了使效果起作用,我需要第一个项目具有特定的类,然后它的兄弟姐妹没有这些类。我尝试使用计数器,但我对 PHP/ACF 比较陌生,所以虽然我可以做基本的中继器工作,但这让我有点卡住了。我尝试过的如下
<?php if( have_rows('homepage_tiles') ):
$count = 0;
?>
<div class="hero-list-container">
<?php while( have_rows('homepage_tiles') ): the_row();
// vars
$image = get_sub_field('image');
$content = get_sub_field('content');
$link = get_sub_field('link');
?>
<?php if($count == 0): ?>
<div class="list-tile animate-up first">
<div class="module-background" style="background-image: url('<?php the_sub_field('tile_bg_image') ?>'); background-size: cover;">
</div>
</div>
<?php $counter++; ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
我会很好地循环并将类添加到第一个,或者输出第一个项目所需的完整 html,然后输出不同的 html。
谢谢您的帮助!
所以我使用了它并且它有效,但如果有人有更好的解决方案,我愿意接受。
<?php if( have_rows('homepage_tiles') ):
$count = 1;
?>
<div class="hero-list-container">
<?php while( have_rows('homepage_tiles') ): the_row();
// vars
$image = get_sub_field('image');
$content = get_sub_field('content');
$link = get_sub_field('link');
?>
<?php if($count == 1): ?>
<div class="list-tile animate-up first">
<div class="module-background" style="background-image: url('<?php the_sub_field('tile_bg_image') ?>'); background-size: cover;">
</div>
</div>
<?php endif; ?>
<?php $count++; ?>
<?php if($count > 2): ?>
<div class="list-tile">
<div class="module-background" style="background-image: url('<?php the_sub_field('tile_bg_image') ?>'); background-size: cover;">
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
我将它设置为 >= 2 但由于某种原因它会重复第一张幻灯片。
解决方案
我意识到这个问题是几个月前发布的,但如果你想稍微简化你的代码,这是我的解决方案。
<?php if( have_rows('homepage_tiles') ): $counter = 0; ?>
<div class="hero-list-container">
<?php
while( have_rows('homepage_tiles') ): the_row();
$tile_bg_image = get_sub_field('tile_bg_image');
?>
<div class="list-tile<?php if( $counter == 0 ) { ?> animate-up first<?php } ?>">
<div class="module-background" style="background-image: url('<?php echo $tile_bg_image; ?>'); background-size: cover;"></div>
</div>
<?php $counter++; endwhile; ?>
</div>
<?php endif; ?>
推荐阅读
- javascript - 在错误的位置打开图层 WMTS
- node.js - 无法通过订阅更新商店,因为 ConnectionHandler.getConnection 总是返回 undefined
- angular - Angular:如何测试单击组件 nativeElement 会打开一个对话框
- bash - Bash脚本将环境变量作为命令传递但不执行它?
- ruby-on-rails - 如何在 Ruby on Rails 表单中添加 document.getElementById 值?
- vue.js - 从输入 vue 中获取日期
- javascript - Angular 10:我正在尝试从跨度复制字符串数据,而不使用 HTML 中的 @Input 括号
- r - 使用 R 中的数据表在值旁边呈现彩色箭头
- c# - Aspose PDF 以pdf形式添加保存按钮
- reactjs - 在 reactjs 中, require() 没有加载本地图像?