“Warning: count()” error on php 7.2

I’m currently working with php 7.2 and when I use get_the_content() or get_the_excerpt() outside of a single template, in the functions.php for example, I get the following Warning:

Warning: count(): Parameter must be an array or an object that implements Countable in /Applications/MAMP/htdocs/wordpress/wp-kona/wp-includes/post-template.php on line 284

What’s wrong with it? Is it a wordpress core bug? Am I missing something.

4 s
4

Yes, I already knew the cause of what you’re experiencing:

The $page and $pages global variable have not been set up and a call to get_the_content or get_the_excerpt would return that error: count(pages)

That said, pages global variable is set to null, calling count(pages) triggers that error because a null value cannot be counted.

To fix this, one can easily tell that you’re calling get_the_content or get_the_excerpt outside the loop.

So you have to set up the post data correctly before usage, that’s what the loop is meant to do:

while ( have_post(): the_post() ) :
    // get_the_content() or get_the_excerpt() will work fine now
    the_content();
endwhile;

Leave a Comment