カスタム投稿タイプで遊んでいて、アーカイブページでコメントを表示させようと、wp_list_comments()
関数を使ったらうんともすんとも言わない。で、テストサイトで関数の中に直接ログを吐き出させるコードを挿入したりして調べると、途中の条件チェックで何も返さず return;
してる。どうもグローバル変数の $wp_query
の $comments
プロパティ ( $wp_query->comments
) が空なのが原因のよう。

解決方法
これは、get_comments()
か WP_Comment_Query
を使って独自でコメントのクエリをかけてコメントオブジェクトを格納する配列を取得し、wp_list_comments()
の2番めのパラメーターに渡してあげれば良い。
こんな感じ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$_aComments = get_comments( array( 'post_id' => get_the_ID(), 'status' => 'approve', ) ); wp_list_comments( array( 'type' => 'comment', 'avatar_size' => 40, 'style' => 'ol', 'short_ping' => true, ), $_aComments ); |
結果
うまくいくとアーカイブページで記事リストしながらコメントも一緒に表示させることが可能。

参考:https://deluxeblogtips.com/display-comments-in-homepage/