不具合内容
WordPressのバージョンを更新するたびに同じエラーが出るので、自分のメモを兼ねて対処法を残しておきます。
以下のようなエラーコードがサイトの上部に表示されていました。
Warning: count(): Parameter must be an array or an object that implements Countable in /home/xxxxx/www/wp/wp-includes/post-template.php on line 284
表示では以下のように表示されています。
原因
PHPのバージョンが7.2以上のサーバーにおいて発生するようです。
WordPressがサポートしているPHPのバージョンが5.6以上になったこともあり、古いバージョンが使えなくなってきています。
なので、同様のエラーに遭遇している方も多いかと思います。
このエラーが出る原因は、PHP7.2で count()関数の仕様が変更になったからだそうです。
特に重大なエラーではなさそうです。
原因となっているコードが書かれているファイルは、以下のディレクトリに存在しています。
. └── wp(WordPressのインストールフォルダ) └── wp-includes └── post-template.php
対処法
以下のページに解決策が書かれていました。便利なので、引用させていただきます。
そのままコピペしたら解決します。感謝O(-人-)O
修正箇所は、上記のディレクトリの「post-template.php」のファイルの284行目に存在しています。
Windowsであれば、「Ctrl + F」で「post-template.php」のファイル内を『post_password_required』などで検索すると探しやすいかと思います。
<修正前>
// 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; }
(引用元)
WordPressでphp7.2にした時にWarning: count()が出た場合の治し方(コピペ可)
WordPressのバージョンを更新するたびに「post-template.php」のファイルが更新されるようで、更新のたびに同じエラーが出ます…。
面倒くさい…(-_-;)
コメント
私のは
if ( $elements[‘page’] > count( $elements[‘pages’] ) ) { // if the requested page doesn’t exist
$elements[‘page’] = count( $elements[‘pages’] ); // give them the highest numbered page that DOES exist
}
となっているのですが修正方法が解りません…
すいません、私も分かりません…
TeratailなどのQAサイトで聞いてみてはいかがでしょうか?