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



JavaScriptのテンプレートリテラル内で条件分岐を行う方法
CodeAnywhereでターミナルに触る練習をする
Google Maps APIでピンの見た目をカスタマイズする方法
Font Awesome 5の基本的な使い方と、Font Awesome 4との違い
コーディング時にちゃんと意識したい複数語の連結方法一覧(キャメルケース・スネークケース・ケバブケース)
JavaScriptのforEach内で「continue」や「break」のような動作をさせる方法
iCLUSTA+でWordPressのアップロード容量を上げたいときのメモ
ウェブ上でLaTeXで数式をキレイにプレビューするJavaScriptライブラリ「MathJax」
1Passwordの開発者向け機能(SSH Agent / Environments / 1Password CLI / Developer Watchtower)
AirPodsで片耳を外しても再生が止まらないようにする方法
MacのKeynoteにハイライトされた状態でコードを貼り付ける方法
iTerm2でマウスやトラックパッドの操作を設定できる環境設定の「Pointer」タブ
DeepLで「インターネット接続に問題があります」と表示されて翻訳できないときに確認すること
Ulyssesの「第2のエディタ」表示を使って2つのシートを横並びに表示する
macOSのシステム環境設定「アクセシビリティ」内にあるVoiceOverの設定見直し
Sketchのメニューバーにショートカットキーを割り当てる方法
Keynote × Keyboard Maestroでスライド作りを徹底的にラクにするマクロサンプル
1Passwordでウェブサイトの入力フォームを一瞬で入力する方法
Figmaのテキストやオブジェクトを斜めに変形・旋回させるプラグイン「SkewDat」
Figmaで細かいアニメーションを作成できるプラグイン「Motion」
EvernoteとBearの違いや使い分けのヒント
QuickTime Playerとプレビュー.appで動画の一部を画像として書き出す方法
Vue CLIでVue.jsの開発環境をセットアップするまでの流れ
Macのアプリインストールとアンインストール方法まとめ