【问题标题】:PHP Error: Parameter must be an array or an object that implements CountablePHP 错误:参数必须是数组或实现 Countable 的对象
【发布时间】:2022-01-04 15:14:27
【问题描述】:

我使用教程从 github 上的这个要点创建可过滤的投资组合 > archive-portfolio.php on https://gist.github.com/About2git/1f47cdadce99d886700e

我在我的页面上看到一条错误消息: 警告:count():参数必须是数组或对象,在第 40 行的 archive-portfolio.php 中实现 Countable。

第 40 行是 $count = count($terms);完整的功能是:

//* Display Picnics Categories
add_action( 'genesis_before_loop', 'sk_isotope_filter' );
function sk_isotope_filter() {

    if ( is_post_type_archive( 'picnics' ) )

        $terms = get_terms( 'picnics_category' );
        $count = count($terms);
        $count = $countarray[0];
        $i=0;
        if ($count > 0) { ?>
            <ul id="picnics-cats" class="filter clearfix">
                <li><a href="#" class="active" data-filter="*"><span><?php _e('All', 'genesis'); ?></span></a></li>
                <?php foreach ($terms as $term ) : ?>
                    <li><a href="#" data-filter=".<?php echo $term->slug; ?>"><span><?php echo $term->name; ?></span></a></li>
                <?php endforeach; ?>
            </ul><!-- /picnics-cats -->
        <?php }
}

我已经尝试获取编辑器一段时间但没有任何响应,希望有人可以帮助提供正确的代码。此错误仅在 PHP 版本更新后出现。

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    您所指的教程也没有使用较新版本的 PHP (v7.3+),因此您可以“逃避”不检查变量是否可数,但在较新的版本中,这已得到修复现在,在您可以count() 变量之前,您需要首先检查它是否是您实际可以计算的值。

    您需要先检查$terms 是否可数,然后才能count

    添加了is_countable 函数来解决这个确切的问题,you can read more about it here

    【讨论】:

    • 感谢您的回复 Duenna,非常感谢。我意识到我发布了一个分叉的要点,而原始要点在这里 > gist.github.com/srikat/9406477 我尝试参考您的链接更改代码但没有成功。不幸的是,我的 PHP 不好。我已再次向编辑器发送消息以协助更新代码。希望他能回到我身边。
    猜你喜欢
    • 2020-08-15
    • 2019-06-20
    • 1970-01-01
    • 2018-11-18
    • 2019-08-03
    • 2019-08-15
    • 2019-05-10
    • 2020-05-28
    相关资源
    最近更新 更多