最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Warning: count(): syntax problem of the count () function

programmeradmin2浏览0评论

I'm trying to repair a function that was working properly before going to php 7.2. The error comes from the count () function but I do not know how to rewrite it. Can you help me ?

Here is the error message: Warning: count(): Parameter must be an array or an object that implements Countable in...

This function is a part of the code for displays a page above a category.

  • is_category: If the current page is a category
  • id : category identifier
  • title : Category title
private function get_category($id_cat = false){
        $Category = new stdClass();
        $Category->is_category = false;
        $Category->id = 0;
        $Category->title = '';
        if (( is_category())||(is_tax('portfolio_categories'))) {
            $Category->is_category = true;
        }
        if ($id_cat === false) {
            $cat = single_cat_title("",false);
        }
        else{
            if ( (int) $id_cat > 0) {
                $cat = get_cat_name($id_cat);
            }
            else{
                return $Category;
            }
        }
        $page_id = false;
        $titre_page = sanitize_title($cat);

        global $wpdb;
        $req = $wpdb->prepare("SELECT ID FROM {$wpdb->prefix}posts WHERE post_name=%s AND post_content != '' AND post_type = 'page' AND post_status = 'publish'", $titre_page);
        $page = $wpdb->get_row($req);
        $Category->id = (count($page) > 0) ? $page->ID : 0;
        $Category->title = $titre_page;
        return $Category;
    }

I am not a php developer, so thank you for your indulgence

I'm trying to repair a function that was working properly before going to php 7.2. The error comes from the count () function but I do not know how to rewrite it. Can you help me ?

Here is the error message: Warning: count(): Parameter must be an array or an object that implements Countable in...

This function is a part of the code for displays a page above a category.

  • is_category: If the current page is a category
  • id : category identifier
  • title : Category title
private function get_category($id_cat = false){
        $Category = new stdClass();
        $Category->is_category = false;
        $Category->id = 0;
        $Category->title = '';
        if (( is_category())||(is_tax('portfolio_categories'))) {
            $Category->is_category = true;
        }
        if ($id_cat === false) {
            $cat = single_cat_title("",false);
        }
        else{
            if ( (int) $id_cat > 0) {
                $cat = get_cat_name($id_cat);
            }
            else{
                return $Category;
            }
        }
        $page_id = false;
        $titre_page = sanitize_title($cat);

        global $wpdb;
        $req = $wpdb->prepare("SELECT ID FROM {$wpdb->prefix}posts WHERE post_name=%s AND post_content != '' AND post_type = 'page' AND post_status = 'publish'", $titre_page);
        $page = $wpdb->get_row($req);
        $Category->id = (count($page) > 0) ? $page->ID : 0;
        $Category->title = $titre_page;
        return $Category;
    }

I am not a php developer, so thank you for your indulgence

Share Improve this question edited Jun 12, 2019 at 10:25 Bunö asked Jun 12, 2019 at 10:16 BunöBunö 34 bronze badges 2
  • If you’re not a developer, where is this code from? A plugin? If it’s a plugin you should ask the author to fix it. – Jacob Peattie Commented Jun 12, 2019 at 10:32
  • This is the problem of homemade code when the developer leaves the ship. – Bunö Commented Jun 12, 2019 at 12:25
Add a comment  | 

2 Answers 2

Reset to default 0

Try replacing this in the original code:

$page = $wpdb->get_row($req);

with this:

$page = $wpdb->get_row($req, ARRAY_A);

This will make it return an array instead of an object.

Then you can follow with this:

if ($page && isset($page['ID'])) {
    $category->id = $page['ID'];
} else {$category->id = 0;}

I replaced the incriminated line

$Category->id = (count($page) > 0) ? $page->ID : 0;

with

if (isset($page)){
$Category->id = $page->ID;
}
else {
$Category->id = 0;
}

I'm not sure of the syntax, but it works

Documentation : [https://www.php/manual/fr/function.count.php][1]

发布评论

评论列表(0)

  1. 暂无评论