【问题标题】:wordpress hero invalid argument supplied for foreach()为 foreach() 提供的 wordpress hero 无效参数
【发布时间】: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 函数——我假设这里的返回类型不是一个数组。

标签: php wordpress foreach


【解决方案1】:

检查$images is_iterable

if(is_iterable($images)) {
    // foreach gets indented here
}

另请参阅 is_iterable http://php.net/manual/en/function.is-iterable.php 的手册页

一个不错的 PHP5 用户的 polyfill(希望他们不在

<?php
if(!function_exists('is_iterable')) {
    function is_iterable($var) {
        return ( is_array( $var ) || ( $var instanceof Traversable ) );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 2011-02-07
    相关资源
    最近更新 更多