【发布时间】:2017-11-01 22:41:35
【问题描述】:
我注意到我的 hero.php 文件中的 div image-16-8 没有显示出来。
当我检查元素时,我得到 Invalid argument provided for foreach() in
代码如下:
<ul data-simple-slider>
<?php foreach ($images as $image): ?>
<li>
<div class="image-16-8" style="background-image: url(<?= $image->url; ?>); "></div>
</li>
<?php endforeach; ?>
</ul>
我读过similiar question,其中在foreach 之前添加if 条件似乎可以解决问题。
由于我是 php 的初学者,我不知道该怎么做。
非常感谢任何帮助!
更新这是完整的英雄 div -
<?php
function tbhHeroShortcode($atts)
{
$values = shortcode_atts(array(
'images' => '',
'first-line' => '',
'second-line' => '',
'video' => '',
'link' => '',
), $atts);
ob_start();
?>
<div class="hero">
<?
$images = decode_shortcode_data($values['images']);
if ($images): ?>
<ul data-simple-slider>
<?php foreach ($images as $image): ?>
<li>
<div class="image-16-8" style="background-image: url(<?= $image->url; ?>); "></div>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<div class="hero-content">
<div class="hero-content-first-line">
<h1 class="header"><?= decode_shortcode_data($values['first-line']) ?></h1>
</div>
<h1 class="italic-header"><?= decode_shortcode_data($values['second-line']) ?></h1>
<div class="hero-content-cta">
<a class="hollow-button" href="<?= decode_shortcode_data($values['link']) ?>">Learn More</a>
</div>
</div>
<?php if (count($images) > 1): ?>
<div class="hero-controls">
<i class="fa fa-chevron-left hero-controls__left" aria-hidden="true"></i>
<i class="fa fa-chevron-right hero-controls__right" aria-hidden="true"></i>
</div>
<?php endif; ?>
</div>
<?php
$component = ob_get_contents();
ob_end_clean();
return $component;
}
add_shortcode('tbhHero', 'tbhHeroShortcode');
【问题讨论】:
-
这意味着您的
$images不是变量,不能循环 - 检查您设置的位置。 -
好的,谢谢,查看更新后的问题,我添加了代码来显示
$images的设置位置。 -
可以发
decode_shortcode_data()函数 -
我已经添加了完整的代码,所以你可以看到
decode_shortcode_data() -
@JordanMiguel 你实际上并没有发布你的 decode_shortcode_data 函数——我假设这里的返回类型不是一个数组。