PHP warning after install

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!

Any ideas would be much appreciated.

5 Likes

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.

And welcome to CP… good to have you here!

4 Likes

Could it be the theme?
I mean, nowadays themes have all that gb related bloat…
Have you tried to use a default theme?

1 Like

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?

Welcome @MoJones. As has been discovered in this thread this is caused by theme code doing something unexpected. It’s usually also a good idea to search for these error messages and see if they have been reported by WP users. In this case: https://core.trac.wordpress.org/ticket/47824

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.

  • Before: the_content();
  • After: $GLOBALS['pages'] = array(); the_content();

Hope that helps… at least, that should be enough to avoid the current warning if I’ve got my understanding of the issue right.

6 Likes

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.

4 Likes

James, you are a genius. I made two changes [edit: to the_content functions] as you instructed to the single.php file - and that fixed it! Thank you.

This place is amazing. I’m so glad I found you.

6 Likes

Marking as solved. Cheers. :slight_smile:

3 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.