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





全角スペースを可視化してくれるVisual Studio Codeの拡張機能「zenkaku」
WordPressのカスタムメニューでは、内部リンクに対してカスタムリンクは使わない!
CSSやJavaScriptでスクロールバーの横幅を取得する方法
npm-scriptsでSCSSをコンパイルする環境を構築する方法
ダミーとして使うURLは適当に入力せず、決まったものを使うようにしよう
アクセス解析ツールの定番!!Googleアナリティクスの登録方法
Webデザイナーを目指す専門学生が技術ブログを書くメリット
JavaScriptのテンプレートリテラル内で条件分岐を行う方法
MacのKeynoteにハイライトされた状態でコードを貼り付ける方法
AirPodsで片耳を外しても再生が止まらないようにする方法
iTerm2でマウスやトラックパッドの操作を設定できる環境設定の「Pointer」タブ
DeepLで「インターネット接続に問題があります」と表示されて翻訳できないときに確認すること
Ulyssesの「第2のエディタ」表示を使って2つのシートを横並びに表示する
macOSの効率化に関するDiscordコミュニティ「CMD-F」を立ち上げました
1Passwordの開発者向け機能(SSH Agent / Environments / 1Password CLI / Developer Watchtower)
Default Folder Xで保存ダイアログ内のファイルリストにフォーカスする方法
Obsidianで整理した知識をAnkiで定着させる方法
Figma × Keyboard Maestroでどこまで効率化できる?
マウス操作の完成度が高いウインドウリサイズアプリ「Magnet」
Macで複数ディスプレイを使っている場合に便利な設定集
AppCleanerでMacアプリを関連ファイルごと完全に削除する
操作に没頭できる!Firefox版 Arcのようなブラウザ「Zen Browser」
chezmoi(シェモア)で始めるdotfiles管理