![](/img/trans.png)
[英]PHP error: Warning: count(): Parameter must be an array or an object that implements Countable
[英]PHP Error - Warning: count(): Parameter must be an array or an object that implements Countable
完整的错误是:“警告:count():参数必须是在第 54 行的 /app/public/wp-content/themes/site/inc/lists_posts.php 中实现 Countable 的数组或对象”
它说的是第 54 行,这是第一个 if 语句开始的地方..知道如何解决这个问题吗?
<?php foreach($projects as $key => $project){
$display = true;
$terms = get_the_terms($project->ID,"diadem_project_category");
if(count($terms) == 1){
// function located in function.php
$display = check_project_display($terms[0]->term_id);
}
if($display){
$permalink = get_permalink($project->ID);
$project_caption = get_field('project_caption',$project->ID);
$type = get_field('type',$project->ID);
$subscript_number = get_field('subscript_number',$project->ID);
$download_pdf = get_field('download_pdf',$project->ID);
$thumbnail = get_the_post_thumbnail_url($project->ID);
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id($project->ID), 'medium');
$categories = wp_get_post_terms($project->ID, $category_name, array("fields" => "all"));
$target ="";
$slug = "";
if($categories){
foreach($categories as $cat){
$slug .= $cat->slug." ";
if($category_name == "diadem_insight_category"){
$project_caption = $cat->name;
}
}
}
$project->post_title = str_replace("'","’",$project->post_title);
?>
有时在获取数据时,会发生一些错误,返回false
而不是结果集。 所以在检查数据长度之前,我们也应该比较输出类型。
if($terms !== false && count($terms) == 1){
// function located in function.php
$display = check_project_display($terms[0]->term_id);
}
而且我认为您可能会得到n
术语,而不仅仅是一个。 在这种情况下
if($terms !== false && count($terms) >= 1){
// function located in function.php
$display = check_project_display($terms[0]->term_id);
}
首先使用var_dump($terms)
或echo gettype($terms)
检查变量$terms
的数据类型。它必须是数组或对象,然后才不会发生警告。您使用的count()
方法期望参数数据类型应该是array
或object
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.