質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

Q&A

解決済

1回答

854閲覧

トップページのサイドバーの「最新のコメント」にカスタム投稿のコメントが表示されてしまう

_catbird284

総合スコア3

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

0グッド

0クリップ

投稿2021/10/13 12:15

Wordpressで2カラムのブログページを作成しています。
カスタム投稿「memorandum」を作成しました。
カスタム投稿「memorandum」のページのサイドバーの「最新のコメント」ブロックには「memorandum」の記事につけられたコメントだけを、
トップページの投稿ページのサイドバーの「最新のコメント」ブロックには「post」の記事につけられたコメントだけを表示したいです。

カスタム投稿「memorandum」のページのサイドバーのコメントブロックに「memorandum」の記事につけられたコメントだけを表示することはできたのですが、
トップページのサイドバーのコメントブロックには「post」だけでなく「memorandum」の記事につけられたコメントも表示されてしまいます。

ダッシュボードの「外観」>「ウィジェット」で、ブロック「最新のコメントを追加」を選ぶとそうなってしまいます。

comment.phpを編集したのですが変化がなかったためコードが読み込まれていない可能性があります。
どこのコードを変えればテーマのウィジェットの「細心のコメント」ブロックが改変できるのか分かりません。
サイドバーそのものを自作するしかないのでしょうか?

comment.php(コメントの部分のみ編集):

PHP

1<?php 2/** 3 * The template for displaying comments. 4 * 5 * The area of the page that contains both current comments 6 * and the comment form. 7 * 8 * @package Nisarg 9 */ 10 11/* 12 * If the current post is protected by a password and 13 * the visitor has not yet entered the password we will 14 * return early without loading the comments. 15 */ 16if ( post_password_required() ) { 17 return; 18} 19?> 20 21<div id="comments" class="comments-area"> 22 23 <?php if ( have_comments() ) : ?> 24 <h2 class="comments-title"> 25 <?php 26 $nisarg_comment_count = get_comments_number(); 27 if ( '1' === $nisarg_comment_count ) { 28 printf( 29 /* translators: 1: title. */ 30 esc_html__( 'One thought on &ldquo;%1$s&rdquo;', 'nisarg' ), 31 '<span>' . wp_kses_post( get_the_title() ) . '</span>' 32 ); 33 } else { 34 printf( 35 /* translators: 1: comment count number, 2: title. */ 36 esc_html( _nx( '%1$s thought on &ldquo;%2$s&rdquo;', '%1$s thoughts on &ldquo;%2$s&rdquo;', $nisarg_comment_count, 'comments title', 'nisarg' ) ), 37 number_format_i18n( $nisarg_comment_count ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 38 '<span>' . wp_kses_post( get_the_title() ) . '</span>' 39 ); 40 } 41 ?> 42 </h2> 43 <ol class="comment-list"> 44 <?php 45 $comments = get_comments(array('post_type' => 'post',)); // 投稿タイプを指定する記述を追加 46 wp_list_comments( array( 47 'style' => 'ol', 48 'short_ping' => true, 49 'avatar_size' => 50, 50 ), $comments ); // 記述を追加 51 ?> 52 </ol><!-- .comment-list --> 53 54 <?php 55 // Are there comments to navigate through? 56 if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : 57 ?> 58 <nav class="navigation comment-navigation" role="navigation"> 59 <h2 class="screen-reader-text"><?php esc_html_e( 'Comment navigation', 'nisarg' ); ?></h2> 60 <div class="nav-links"> 61 <?php 62 if ( $prev_link = get_previous_comments_link( __( 'Older Comments', 'nisarg' ) ) ) : 63 printf( '<div class="nav-previous">%s</div>', $prev_link ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 64 endif; 65 66 if ( $next_link = get_next_comments_link( __( 'Newer Comments', 'nisarg' ) ) ) : 67 printf( '<div class="nav-next">%s</div>', $next_link ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 68 endif; 69 ?> 70 </div><!-- .nav-links --> 71 </nav><!-- .comment-navigation --> 72 <?php endif; // Check for comment navigation ?> 73 74 <?php if ( ! comments_open() && get_comments_number() ) : ?> 75 <p class="no-comments"><?php esc_html_e( 'Comments are closed.' , 'nisarg' ); ?></p> 76 <?php endif; ?> 77 78 <?php endif; // have_comments() ?> 79 <?php comment_form(); ?> 80</div><!-- #comments --> 81

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

comment.phpじゃなくcomments.phpじゃないの?
そもそもcomments.phpはサイドバーには関係ない。

widget_comments_argsで条件分岐すればどうにでもなる。
https://developer.wordpress.org/reference/hooks/widget_comments_args/

投稿2021/10/14 03:33

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

_catbird284

2021/10/15 13:32

function.phpに以下のコードを追加したらできました。ありがとうございました。 add_filter( 'widget_comments_args', 'exclude_memorandumComments'); function exclude_memorandumComments( $args ){ $args["post_type"] = 'post'; return $args; }
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問