3

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.

  • What is your full use of the function? – Jacob Peattie Mar 21 '18 at 11:45
  • 1
    Are you calling these functions outside the loop? – kero Mar 21 '18 at 11:47
  • I think this is unimportant. I'm just using it to check if the content has some specefic shortcode for example. Yes, it's outside the loop. But how can I access to the content (with the id) then outside the loop. – Benmay Mar 21 '18 at 11:49
  • 1
    Can you show the specific usage that is causing the issue. To rule out any syntax issues or incorrect usage of the function. – Jacob Peattie Mar 21 '18 at 11:50
  • This is a bug in core WP. See core.trac.wordpress.org/ticket/42814 – Nathan Johnson Mar 21 '18 at 15:23
0

This bug related to WordPress core bug https://core.trac.wordpress.org/ticket/42814

Most often it occurs when you call the function get_the_excerpt or get_the_content outside the Loop provided that excerpt or content is empty.

So to fix it yourself you should find error place and check excerpt/content existing manually.

For example:

if(has_excerpt($post_id)) {
    $meta[] = sprintf('<meta name="description" content="%s">',
        get_the_excerpt($post_id)
    );
}
  • Please see this comment on the page you linked to: core.trac.wordpress.org/ticket/42814#comment:16 - seems like this exact issue. They still can not reproduce it as an issue with WP core. I my memory serves me right count() used to show a notice if you gave it a string, now it seems to be a warning. – Justin Waulters Jan 3 at 23:44
0

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 call 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 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;
  • This answer does not address the OP's PHP error related to the count() function. – Justin Waulters Jan 3 at 23:27
  • I've already stated why count() trigger that error. The http://php.net/manual/en/function.count.php count() function counts all elements in an array or something in an object. Anything else will throw an error. – John Zenith Jan 4 at 6:25
  • @benmay to explore this error, set the $page and $pages global var before calling get_the_content() or get_the_excerpt() and see what happens next: global $post; $page, $pages; $pages = 1; $page = 1; Now call get_the_content() and see for yourself. – John Zenith Jan 4 at 18:11
  • Thanks for helping me see that answer John! I deleted my answer because it was based on a misunderstanding of the issue and didn't solve the problem. – Justin Waulters Jan 5 at 15:03
  • @JustinWaulters you are welcome. Glad I could be of little help – John Zenith Jan 10 at 2:07

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.