PHP7.2のWordPressでWarning: count(): Parameter must be an array or an object that implementsが表示された時の治し方
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」とエラーメッセージが表示されることがあります。

今回はその治し方についでです。
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.
今回の場合はcount($pages)の$pagesの値が空(NULL)になっていたため、エラーになってしまいました。
ですから、if(!empty)で空ではないという条件を加えて、回避したというわけです。



CodeAnywhereでターミナルに触る練習をする
Google Maps APIをローカル開発環境で使用するときの制限設定方法
Google Maps APIでピンのアクティブ時にピン画像を変える方法
「epel-release」インストール後にyum installで「Cannot retrieve metalink for repository」と表示される場合の対処法
Visual Studio Codeで簡易的なローカルサーバーを起動して、コード保存時に自動でブラウザをリロードする拡張機能「Live Server」
CSSで::placeholderにスタイルを設定する方法
Google Chromeでinput要素の補完機能を使うと背景に色がついてしまう問題
Bracket Pair ColorizerでVisual Studio Codeの括弧を色分けして見やすくする
Visual Studio CodeでDart Sassのコンパイルができる拡張機能「DartJS Sass Compiler and Sass Watcher」
iTerm2で「Use System Window Restoration Setting」を設定しているとアラートが表示されて機能しない
Google Chromeのサイト内検索(カスタム検索)機能を別のプロファイルに移行する方法
iPadで入力モードを切り替えずに数字や記号をすばやく入力する方法
iPhoneやiPadでYouTubeの再生速度を3倍速や4倍速にする方法
Keynoteで有効にしているはずのフォントが表示されない現象
MacのKeynoteにハイライトされた状態でコードを貼り付ける方法
AirPodsで片耳を外しても再生が止まらないようにする方法
iTerm2でマウスやトラックパッドの操作を設定できる環境設定の「Pointer」タブ
DeepLで「インターネット接続に問題があります」と表示されて翻訳できないときに確認すること
Ulyssesの「第2のエディタ」表示を使って2つのシートを横並びに表示する
Obsidianで整理した知識をAnkiで定着させる方法
Figma × Keyboard Maestroでどこまで効率化できる?
マウス操作の完成度が高いウインドウリサイズアプリ「Magnet」
Macで複数ディスプレイを使っている場合に便利な設定集
AppCleanerでMacアプリを関連ファイルごと完全に削除する
操作に没頭できる!Firefox版 Arcのようなブラウザ「Zen Browser」
chezmoi(シェモア)で始めるdotfiles管理
GitHubの通知を見逃さない!通知に特化したアプリ「Gitify」
Keyboard Maestroの変数基礎
Neovimを使い始める最初の一歩として便利な「LazyVim」