最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

"Warning: count()" error on php 7.2

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked Mar 21, 2018 at 11:42 BenmayBenmay 3617 silver badges17 bronze badges 7
  • What is your full use of the function? – Jacob Peattie Commented Mar 21, 2018 at 11:45
  • 1 Are you calling these functions outside the loop? – kero Commented Mar 21, 2018 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 Commented Mar 21, 2018 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 Commented Mar 21, 2018 at 11:50
  • This is a bug in core WP. See core.trac.wordpress/ticket/42814 – Nathan Johnson Commented Mar 21, 2018 at 15:23
 |  Show 2 more comments

4 Answers 4

Reset to default 3

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;

As of v5.2, this will no longer be an issue. get_the_content() and wp_trim_excerpt() are getting an optional new parameter (third and second respectively) of $post so you can use it outside the loop, bringing it in line with the functionality of all the other get_the_* functions. It only took 16 years... Reference on Trac

This bug related to WordPress core bug https://core.trac.wordpress/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)
    );
}

If someone bumps in to this issue, the solution is to get the_content from the meta field and run it trough the_content filter like this:

apply_filters('the_content', get_post_field('post_content', $post->id));

I made a short video explayining the whole thing if you wait untill the TLDR section... https://www.youtube/watch?v=vMguTNzFoUk

发布评论

评论列表(0)

  1. 暂无评论