WordPressでサイドバーに独自の新着コメントをリストを追加
ウィジェットを使えば新着コメントをサイドバーに出す事は可能ですが、デフォルトのHTMLだとカスマイズしにくい場合がります。
その時は以下のソースをサイドバーに追加する事で独自の新着コメントリストを表示できます。これならHTMLも編集できるのでCSSカスタマイズもやりやすくなります。
3 | $comments = get_comments( 'type=comment&status=approve&number=10' ); |
5 | if ( is_array ( $comments ) && count ( $comments ) == 0) echo "コメントはありません。" ; |
9 | foreach ( $comments as $comment ){ |
10 | isset( $posts_list [ $comment ->comment_post_ID])? $posts_list [ $comment ->comment_post_ID]++ : $posts_list [ $comment ->comment_post_ID] = 1; |
12 | while (list( $key , $val ) = each( $posts_list )){ |
13 | $first_comment = true; |
14 | foreach ( $comments as $comment ){ |
16 | if ( $comment ->comment_post_ID != $key ) continue ; |
19 | $comment_post = get_post( $comment ->comment_post_ID); |
20 | $post_title = mb_strimwidth( $comment_post ->post_title, 0, 40, "..." , "UTF-8" ); |
21 | $permalink = get_permalink( $comment ->comment_post_ID). "#comments" ; |
22 | $output .= "<li><a href=\"$permalink\">$post_title</a>\n" ; |
23 | $output .= "\t<ul>\n" ; |
24 | $first_comment = false; |
26 | $commentid = get_permalink( $comment ->comment_post_ID). "#comment-" . $comment ->comment_ID; |
27 | if (mysql2date( 'Ymd' , $comment ->comment_date)== date_i18n( 'Ymd' )) $comment_date = mysql2date( 'H:i' , $comment ->comment_date); |
28 | elseif (mysql2date( 'Y' , $comment ->comment_date)== date_i18n( 'Y' )) $comment_date = mysql2date( 'm/d' , $comment ->comment_date); |
29 | else $comment_date = mysql2date( 'Y/m/d' , $comment ->comment_date); |
30 | if ( $comment ->comment_author) $author = mb_strimwidth( $comment ->comment_author, 0, 30, "..." , "UTF-8" ); |
31 | $output .= "\t\t<li><span class=\"comment_author\"><a href=\"$commentid\" title=\"\">" . $author . "</a> </span>" . |
32 | "<span class=\"comment_date\">( $comment_date )</span></li>\n" ; |
34 | $output .= "\t</ul>\n</li>\n" ; |
表示される時は以下のようになります。
・投稿記事タイトル
コメント者名(投稿日時)
コメント者名(投稿日時)
コメント入力欄