php - 在最后一行之前添加一个部分 ACF get_row_layout()
问题描述
需要帮助这些人,因为我试图在最后一行之前添加一个部分,我使用的是 ACF 灵活内容,并且这些行可以互换,所以即使我更改它,我添加的部分仍然会始终在最后一行之前排
if( have_rows($content_rows) ):
while ( have_rows($content_rows) ) : the_row();
if( get_row_layout() == 'row_one' ):
echo 'First row';
endif;
if( get_row_layout() == 'row_two' ):
echo 'Second row';
endif;
{need to add a div here and it should show before the last row even if its moved}
if( get_row_layout() == 'row_three' ):
echo 'Third row';
endif;
endwhile;
endif;
解决方案
诀窍是在最后一行 div 之前计算所有行并输出
if( have_rows($content_rows) ):
$totalCount = count(get_field($content_rows));
$currentCount = 1;
while ( have_rows($content_rows) ) : the_row();
//add div before the last row
if ($currentCount - 1 === $totalCount) {
echo "<div></div>";
}
if( get_row_layout() == 'row_one' ):
echo 'First row';
endif;
if( get_row_layout() == 'row_two' ):
echo 'Second row';
endif;
if( get_row_layout() == 'row_three' ):
echo 'Third row';
endif;
$fields_count++;
endwhile;
endif;
推荐阅读
- mysql - 如何在同一数据库上测试 2 个补丁/post api 调用的响应
- c++ - 空指针解引用
- c - 就 Big O 而言,给定函数的时间复杂度应该是多少?
- express - 尝试使用 ejs 构建待办事项列表不断收到 TypeError
- sql - 将日期从 UTC 时区转换为 PST 时区(包括 DST 因素)
- python - 如何正确更新 django rest 框架中的嵌套对象?
- google-cloud-platform - Bigquery - 使用 json 模式文件创建表
- media-queries - 如何仅删除 Tailwind 2 中的 2xl 断点?
- java - 在 Jetty Client 中处理自动重试
- sql - 如何在phpMyAdmin中创建由CHARACTER + YEAR + ID(自动增量)组成的数据库列?