designserv Posted August 30, 2022 Share Posted August 30, 2022 I've got 2 error messages after I set up a new wordpress installation & installed the knowledge template. It's the first time for me to setup a wordpress page & template. I'm not that known in php and i didn't find a solution for this. So here u can see the error messages live: https://www.kassen.de/knowledge/knowledge-base/ The Errormessages are: Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/37/d157950258/htdocs/knowledge/wp-content/themes/knowledgepress/template-knowledgebase.php on line 98 and Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/37/d157950258/htdocs/knowledge/wp-content/themes/knowledgepress/template-knowledgebase.php on line 116 And this is the code from the php files on the specific lines: Line98: if(count($subcategories)==0 && count($cat_posts)==0){ continue; } Line 116: if(count($subcategories)>0) { foreach($subcategories as $sub_category) { $term_id[] = $sub_category->term_id; ?> Does someone has a solution for this? This would be awesome! Greetings :) Quote Link to comment https://forums.phpfreaks.com/topic/315251-two-error-messages-in-a-php-template-with-wordpress/ Share on other sites More sharing options...
maxxd Posted August 30, 2022 Share Posted August 30, 2022 Seeing as $subcategories is the common variable on both lines, I'd assume that is the problem. As of 7.2, php starting emitting a warning if the variable passed to count() isn't an array or another type that actually can be counted. I don't know what the 'knowledge template' is, but I'd bet it was developed before php7, and that sometimes there are no subcategories - this is probably leaving $subcategories null or 0, which isn't countable. Check to see if there's an updated version of the theme; if not you'll need to find where $subcategories is defined and make sure it's an array even if there aren't any subcategories to be found. Quote Link to comment https://forums.phpfreaks.com/topic/315251-two-error-messages-in-a-php-template-with-wordpress/#findComment-1599913 Share on other sites More sharing options...
designserv Posted August 30, 2022 Author Share Posted August 30, 2022 So this is the template for wordpress. https://themeforest.net/item/knowledge-base-helpdesk-wiki-faq-wordpress-theme/3434096 On the webserver i use MariaDB 10.5. The problem is, i've never done something with php. Only with html, css. I added categories in the wordpress backend but im not sure where this error comes from, or how i can fix that. 😕 Quote Link to comment https://forums.phpfreaks.com/topic/315251-two-error-messages-in-a-php-template-with-wordpress/#findComment-1599914 Share on other sites More sharing options...
ginerjm Posted August 30, 2022 Share Posted August 30, 2022 (edited) The code just prior to those lines needs to check if the subcategories var is an array, or even if it is 'set'. If it is not then you need to code around that logic to avoid the invalid testing that's happening. Actually - upon further review - do a test of the var prior to the use of count and if it is not set add this line: if (!isset($subcategories)) $subcategories = array(); Now you have a countable variable that should solve both errors. Edited August 30, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/315251-two-error-messages-in-a-php-template-with-wordpress/#findComment-1599915 Share on other sites More sharing options...
gizmola Posted August 31, 2022 Share Posted August 31, 2022 Maxxd and ginerjm pretty much nailed it. The warning was introduced in PHP version 7.2. Is it possible you can get an updated version of the template you are using? You might need to make a lot of these types of patches. I'd probably make this variation on ginerjm's fix: $subcategories = empty($subcategories) ? array() : $subcategories; Quote Link to comment https://forums.phpfreaks.com/topic/315251-two-error-messages-in-a-php-template-with-wordpress/#findComment-1599921 Share on other sites More sharing options...
maxxd Posted September 1, 2022 Share Posted September 1, 2022 Just getting back to this. According to the ThemeForest page the last update to this theme was in 2018. A lot has happened in both PHP and WordPress since then - it might be time to start shopping for a new theme. I can almost guarantee that if you're seeing these issues now you're going to see worse issues with the code in the future. Quote Link to comment https://forums.phpfreaks.com/topic/315251-two-error-messages-in-a-php-template-with-wordpress/#findComment-1599952 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.