I know next to nothing about PHP. I had a minor glitch when switching from WP today. A warning across the top of the page:
Warning: count(): Parameter must be an array or an object that implements Countable in /home/public/wp-includes/post-template.php on line 284
It only appears on pages, not the index.
Here’s what’s on lines 284/5 in post-template.php
if ( $page > count( $pages ) ) // if the requested page doesn’t exist
$page = count( $pages ); // give them the highest numbered page that DOES exist
I disabled all the plugins, but that didn’t stop it. I can’t imagine what it could be.
My temporary solution was to comment out those two lines. It worked, and didn’t seem to have any negative effects - but I’m sure that’s not the best solution!
I have had similar messages after migrating sites to CP from WP. In my case it was:
PHP Warning: count(): Parameter must be an array or an object that implements Countable in /home/xxxxxxxx/public_html/wp-content/plugins/sc-email-log/include/UpdateClient.class.php on line 654
It only ever gave this warning once and then seemed happy after that, so I wouldn’t start editing files because of it (also note that it is only a warning, so nothing will crash).
But, I’d also be interested to know why it happens. I’m sure @james will be able to shed some light.
Thanks for the replies. The warning doesn’t go away for me.
You are right, Elisabetta - it is the theme. I switched to TwentyTwenty and the warning went away. So, the solution would be to change the Parameter in that line of code to make it an array or object that implements Countable.
Although, I don’t have a clue what any of that means, I’m guessing that editing or commenting out some code in the my customised theme would fix it.
Could anyone give me a tip about where to start looking?
This issue has been fixed in WP 5.5 (so the same warning would have also appeared in WP 5.4 and earlier). I’ll put it on the list to fix in an upcoming CP release but it depends on some other changes so we’ll need to be careful with that (and we are too close to our upcoming 1.2.0 release so it won’t make it in there).
I wouldn’t recommend changing wp-includes/post-template.php like that - those couple of lines of code have to do with several different scenarios, mostly related to displaying lists of posts across multiple pages. Also, any changes made there will be overwritten on the next upgrade.
Instead if you can find where your theme code calls the_content() or possibly the_excerpt() outside of “the loop” (i.e. not in between while ( have_posts() ) and endwhile) then we can make a modification there. The file to look in depends on where this issue occurs. For example, if you see this warning when viewing single blog posts or pages on your site then you probably want single.php or page.php.
Anyway once you find a call to any of these four functions the_content(), get_the_content(), the_excerpt(), get_the_excerpt() that is not contained within while ( have_posts() ) you should be able to modify that line as follows.
Wow, that is incredibly helpful James. Thank you very much. I will put some time aside soon to have a close look in my theme for those things and see if I can fix this.