【问题标题】:Parameter must be an array or an object that implements Countable参数必须是数组或实现了 Countable 的对象
【发布时间】:2018-11-18 10:10:28
【问题描述】:

我在本地服务器上遇到了这个问题。问题不是由于jetpack插件,因为我已经删除了它

C:\xampp\htdocs\theme\wp-includes\post-template.php on line 284

Warning: count(): Parameter must be an array or an object that implements Countable in post-template.php on line 284

请任何人帮助我解决这个问题。

【问题讨论】:

  • 考虑添加更多细节
  • @RaulRene 自定义代码没有问题,错误来自 WordPress 我尝试安装最新版本的 WordPress,但没有任何变化。我也试过卸载插件,但警告没有消失

标签: php wordpress


【解决方案1】:

如果没有,您是否将 the_content() 置于循环中

这里试试这个:

if (have_posts()) {
    while (have_posts()) {
        the_post();
        ?>
        <article class="post">
        <h3><?php the_title(); ?></h3>
        <p><?php the_content(); ?></p> 
    </article>
    <?php 
} // end while
} // end if
?>

【讨论】:

    【解决方案2】:

    可能是由于在 The Loop 之外使用了 get_the_excerpt、get_the_content 或类似函数(在自定义上下文中,在主题的 functions.php 中的某处等)。

    在前面添加 setup_postdata。这填充了全局变量 $pages。否则 PHP7.2+ 将在第 284 行的 post-template.php 中输出警告。

    例如:

    global $post;
    setup_postdata($post);
    $excerpt = get_the_excerpt($post);
    

    请参阅setup_postdataget_the_excerptThe Loop

    【讨论】:

      【解决方案3】:

      问题来自于在 PHP 7.2+ 中使用 the_content 或 get_the_content,因为这些函数会检查可以为 null 的全局页面。 在 7.2 中,null 不是可数的有效值,这就是导致错误的原因,解决方法是直接从元字段中提取内容。代码如下,更多信息可以在我制作的这个视频中找到https://www.youtube.com/watch?v=vMguTNzFoUk(TLDR部分我猜是在视频中间之后,第一个只是一个sn-p放置说明)

      apply_filters('the_content', get_post_field('post_content', $post->id));
      

      【讨论】:

        猜你喜欢
        • 2019-10-04
        • 2019-07-23
        • 2018-06-28
        • 2018-09-05
        • 2019-09-21
        • 2019-08-03
        • 2019-06-02
        • 2018-06-08
        相关资源
        最近更新 更多