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




Vue CLIでの画像パスの指定方法・配置場所まとめ
WordPress5.5のサイトマップ機能を無効にする
Visual Studio Codeを使ったコード整形方法(Fomatter)
WordPressで「現在メンテナンス中のため、しばらくの間ご利用いただけません。」の状態でログインできなくなった場合の対処法
Visual Studio CodeでToDoコメントを一覧で表示する拡張機能「Todo Tree」
Visual Studio Codeの設定を複数の端末間で同期する「Settings Sync」機能の使い方
CSS疑似要素(::beforeや::after)のcontent内で改行したり半角スペースを複数入力する方法
Vue CLIでVue.jsの開発環境をセットアップするまでの流れ
SourceTreeを開くと「’git status’ failed with code 69:’」というポップアップが出てくる場合の解決方法
SourceTreeでマージ時に「fatal: You have not concluded your merge (MERGE_HEAD exists).」と表示される場合の対処法
AirPodsで片耳を外しても再生が止まらないようにする方法
MacのKeynoteにハイライトされた状態でコードを貼り付ける方法
iTerm2でマウスやトラックパッドの操作を設定できる環境設定の「Pointer」タブ
DeepLで「インターネット接続に問題があります」と表示されて翻訳できないときに確認すること
Ulyssesの「第2のエディタ」表示を使って2つのシートを横並びに表示する
【2026年版】Karabiner-Elementsで実現する右commandキー活用のススメ
Bartender ProのTop Shelf機能でできること
高機能なスクリーンショットアプリ「CleanShot X」
macOSの効率化に関するDiscordコミュニティ「CMD-F」を立ち上げました
1Passwordの開発者向け機能(SSH Agent / Environments / 1Password CLI / Developer Watchtower)
Default Folder Xで保存ダイアログ内のファイルリストにフォーカスする方法
Obsidianで整理した知識をAnkiで定着させる方法
Figma × Keyboard Maestroでどこまで効率化できる?
マウス操作の完成度が高いウインドウリサイズアプリ「Magnet」
Macで複数ディスプレイを使っている場合に便利な設定集