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

  • 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 Jun 12 at 10:32
  • This is the problem of homemade code when the developer leaves the ship. – Bunö Jun 12 at 12:25
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;}
  • Thanks for the support but with your proposal I have the message : Warning: Creating default object from empty value – Bunö Jun 14 at 14:42
  • hmmm not sure but just noticed you have uppercase $Category and I used $category, if you copied my code without changing it, it may interpret you are trying to create a new object $category instead of changing the value of the existing $Category object. – majick Jun 17 at 9:05
  • Oh, sorry for the capital letter ... it's great, it works, thank you very much. – Bunö Jun 21 at 8:13
  • no problems, upper or lowercase is fine so long as the variable name is consistently throughout the code. feel free to mark the answer as correct if it solved the problem. – majick Jun 24 at 7:00
  • Yes, I voted well, but I do not have enough reputation for my vote to be visible ... (The votes cast by those with less than 15 years of reputation are recorded, but do not change the score displayed publicly.) – Bunö Jun 25 at 8:05
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.net/manual/fr/function.count.php][1]

  • this isn't quite correct, isset just means the variable is set so and so in this case it will always be true, even if $page is false. – majick Jun 12 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.