# PHP7.2のWordPressでWarning: count(): Parameter must be an array or an object that implementsが表示された時の治し方 URL: https://webrandum.net/warning-count-parameter-must-be-an-array-or-an-object-that-implements/ 公開日: 2019-03-18 更新日: 2020-04-05 カテゴリー: コーディング タグ: WordPress, エラー 出典: Webrandum(著者: サイトウ マサカズ) PHP7.2でWordPressを使用していると、「Warning: count(): Parameter must be an array or an object that implements Countable in …サイトのURL/wp-includes/post-template.php on line 284」とエラーメッセージが表示されることがあります。 ![](https://webrandum.net/mskz/wp-content/uploads/2019/03/image_1-16-1024x52.png) 今回はその治し方についでです。 ## post-template.phpの修正 治すには「wp-includes/post-template.php」を開きます。 284行目付近が下記のようになっていると思います。 ``` // If post password required and it doesn't match the cookie. if ( post_password_required( $post ) ) return get_the_password_form( $post ); if ( $page > count( $pages ) ) // if the requested page doesn't exist $page = count( $pages ); // give them the highest numbered page that DOES exist ``` それを下記のように変更すればエラーメッセージは消えます。 ``` // If post password required and it doesn't match the cookie. if ( post_password_required( $post ) ) return get_the_password_form( $post ); if ( ! empty( $pages ) ) { if ( $page > count( $pages ) ) // if the requested page doesn't exist $page = count( $pages ); // give them the highest numbered page that DOES exist } else { $page = 0; } ``` ## 原因 PHP7.2からcount関数の仕様が変更されました。 今まではcount(NULL)では何もなく0を返していましたが、PHP7.2からはWarningになるみたいです。 > count() will now yield a warning on invalid countable types passed to the array_or_countable parameter. > PHP: count – Manual 今回の場合はcount($pages)の$pagesの値が空(NULL)になっていたため、エラーになってしまいました。 ですから、if(!empty)で空ではないという条件を加えて、回避したというわけです。