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)で空ではないという条件を加えて、回避したというわけです。



CSSやJavaScriptでスクロールバーの横幅を取得する方法
MacVim-Kaoriyaのインストール方法
Nuxt.jsでVuexストア連携を行う方法|Nuxt.jsの基本
CSS疑似要素(::beforeや::after)のcontent内で改行したり半角スペースを複数入力する方法
CSSで::placeholderにスタイルを設定する方法
ウェブ上でLaTeXで数式をキレイにプレビューするJavaScriptライブラリ「MathJax」
CSSのz-indexの確認や管理を便利にする方法
CodeAnywhereでターミナルに触る練習をする
無料版を使っている人は検討する価値あり!MAMP PRO 5で簡単にローカル開発環境を構築・管理する
iTerm2で「Use System Window Restoration Setting」を設定しているとアラートが表示されて機能しない
Google Chromeのサイト内検索(カスタム検索)機能を別のプロファイルに移行する方法
iPadで入力モードを切り替えずに数字や記号をすばやく入力する方法
iPhoneやiPadでYouTubeの再生速度を3倍速や4倍速にする方法
Keynoteで有効にしているはずのフォントが表示されない現象
MacのKeynoteにハイライトされた状態でコードを貼り付ける方法
AirPodsで片耳を外しても再生が止まらないようにする方法
iTerm2でマウスやトラックパッドの操作を設定できる環境設定の「Pointer」タブ
DeepLで「インターネット接続に問題があります」と表示されて翻訳できないときに確認すること
Ulyssesの「第2のエディタ」表示を使って2つのシートを横並びに表示する
Figma × Keyboard Maestroでどこまで効率化できる?
マウス操作の完成度が高いウインドウリサイズアプリ「Magnet」
Macで複数ディスプレイを使っている場合に便利な設定集
AppCleanerでMacアプリを関連ファイルごと完全に削除する
操作に没頭できる!Firefox版 Arcのようなブラウザ「Zen Browser」
chezmoi(シェモア)で始めるdotfiles管理
GitHubの通知を見逃さない!通知に特化したアプリ「Gitify」
Keyboard Maestroの変数基礎
Neovimを使い始める最初の一歩として便利な「LazyVim」
ウェブサイトをmacOSアプリとしてラッピングする「Unite Pro」