Warning: count() parameter must be an array or an object that implements countable

If you are getting this error ‘Warning: count(): Parameter must be an array or an object that implements Countable in’ /wp-includes/post-template.php on line 310 on top of your homepage then it can be due to PHP version or upgrade issue.

First of all relax this error is a warning or notice about some bug in the code of a particular file not a fatal error or internal server error which can stop your website from loading.

Also read: What are WordPress PHP warnings and how to disable them?

Let’s fix this WordPress error, so the code at this line looks like this

if ( $elements[‘page’] > count( $elements[‘pages’] ) ) {
$elements[‘page’] = count( $elements[‘pages’] );

Add this line is_array( $elements[‘pages’] ) && count( $elements[‘pages’] )

New code looks like this

if ( $elements[‘page’] > ( is_array( $elements[‘pages’] ) && count( $elements[‘pages’] ))) {
$elements[‘page’] = count( $elements[‘pages’] );

Replace the old line of code with the new one mentioned above and save the php file.

Now check your website if the issue is resolved.

Incase you have any question regarding this error please comment below.