【发布时间】:2021-11-07 17:26:49
【问题描述】:
我有一个滑块(Bootstrap 4),它可以渲染一些通过 FTP 上传到服务器上的图像。
我正在使用下面的代码在前端渲染这些图像。
<img src="<?php echo get_template_directory_uri(); ?>/files/images/<?php the_sub_field('image'); ?>" class="img-fluid is-slider-item" />
这可行,但问题是在前端也呈现空字段,显示空元素。
这就是现在在 HTML 中呈现“空”幻灯片的方式。
<img src="http://mywebsite.local/wp-content/themes/new/files/images/" class="img-fluid is-slider-item">
工作中的图像在 HTML 中是这样呈现的
<img src="http://mywebsite.local/wp-content/themes/new/files/images/2345672.png" class="img-fluid is-slider-item">
如何防止空幻灯片在前端呈现?
这里是完整的代码
<?php if( have_rows('files') ): ?>
<div class="carousel_bg">
<div class="container">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<?php
$active = 'active';
$num = 0;
while ( have_rows('files') ) : the_row();
?>
<li data-target="#carousel-example-generic" data-slide-to="<?php echo $num ?>" class="<?php echo $active ?>"></li>
<?php
$active = '';
$num += 1;
endwhile; ?>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php
$active = 'active';
while ( have_rows('files') ) : the_row();
?>
<div class="carousel-item <?php echo $active ?> screen08">
<div class="container">
<img src="<?php echo get_template_directory_uri(); ?>/files/images/<?php the_sub_field('image'); ?>" class="img-fluid is-slider-item" />
</div>
</div><!-- /item -->
<?php $active = '';
endwhile;
?>
</div>
</div>
</div>
</div><!-- /row -->
<?php endif; ?>
我尝试将<?php if( have_rows('files') ): ?> 移动到<div class="carousel_bg"> 中,但没有成功
有什么提示吗?
非常感谢!
【问题讨论】:
-
旁注 - 我无法理解
while ( have_rows('files']) ) : the_row();有两个原因:当您在同一行添加the_row();时,替代语法(我喜欢)看起来像是格式错误的三元组。其次,你的两个函数看起来都很神奇,因为没有迹象表明你实际上是如何让数据进出循环的。它闻起来像全局变量。除此之外,如果have_rows()工作正常,我看不出它为什么会给你额外的幻灯片,因为当你没有数据时它会退出。所以我会看看整个have_rows()/the_row()的事情 -
@TimMorton - 这是 Wordpress 和 Advanced Custom fields。他们正在使用
have_rows()和the_row(),就像手册所说的“标准”Wordpress 代码样式。 Wordpress 遍布全球。 -
@MagnusEriksson 果然……我不使用 wordpress,所以我学到了一些新东西。谢谢
标签: php html wordpress frontend advanced-custom-fields