![](/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.