1月 14, 2011
特定ユーザーの記事に対するコメント一覧を出すプラグイン
WordPressで多人数が執筆するサイトを構築しているので、任意のとあるユーザーが書いた記事に対するコメント一覧を出したかったが、それっぽいプラグインはなかなか見つけられなかった。
そんでこちらの方が書いた簡単な最新コメント一覧を出すスクリプトを見つけたから、ちょっと改造したら行けた。
まずは、以下のソースをテーマの「function.php」に貼付ける。
function src_simple_recent_comments_by_id($src_id=1, $src_count=7, $src_length=60, $pre_HTML='<li><h2>Recent Comments</h2>', $post_HTML='</li>') { if( $src_id < 1 ){ if( is_user_logged_in() ){ //Get User ID $current_user = wp_get_current_user(); $src_id = $current_user->ID; }else{ $src_id = 1; } } global $wpdb; $sql = "SELECT DISTINCT ID, post_title, post_author , post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_content FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' AND post_author = $src_id ORDER BY comment_date_gmt DESC LIMIT $src_count"; $comments = $wpdb->get_results($sql); // Generate the output string, prepend and append the HTML specified $output = $pre_HTML; $output .= "\n<ul>"; if (!empty($comments)) { foreach ($comments as $comment) { //comment $tem_comment = $comment->comment_content; if ( mb_strlen( $tem_comment ) > $src_length+1 ){ $tem_comment = mb_substr($tem_comment,0,$src_length).'...'; } //title $tem_title = $comment->post_title; if ( mb_strlen( $tem_title ) > 15 ){ $tem_title = mb_substr($tem_title,0,14).'...'; } $tem_autho = $comment->comment_author; if( $tem_autho == '' ){$tem_autho = "not name";} $output .= "\n\t<li class=\"src_title\">".$tem_title.$dotst."</li>\n\t<li class=\"src_comment\"><a href=\"" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "\" title=\"on " . apply_filters('the_title_rss', $comment->post_title) . "\">" . $tem_autho . "</a>: " . $tem_comment . $dots . "</li>"; } } else { $output .= "<li class=\"src_title\">No comments.</li>"; } $output .= "\n</ul>"; $output .= $post_HTML; // These lines generate the output echo $before_widget . $before_title . $title . $after_title; echo $output; echo $after_widget; }
一覧を表示させたいところにこれを書けばOK。
<?php src_simple_recent_comments_by_id( 1 , 8 , 30 , 'Header Title', 'Footer Title'); ?>
引数1:指定ID。このIDが書いた記事に対するコメント一覧を出す。0以下の数字にすればログインした人のIDとなる。ログインしていなければデフォルト1になる。
引数2:何件を出すか。デフォルトは7件。
引数3:コメントの文字数。デフォルトは30文字。
引数4:リストの頭に出す文字。
引数5:リストの最後に入れる文字。
元ソースは記事タイトルが入ってないが、個人的何の記事に対するコメントが分からないと不安だから、タイトルを入れることにした。
これで終わりだが、一応これプラグイン化した人も居たから、プラグインバージョンも作っといた。プラグインフォルダに入れて管理画面で有効化すればOK。あとはWidget画面で「src_simple_recent_comments_by_id」というWidgetを設置して上の引数と同じ要領で初期設定すれば終了。
元ソースを書いた人に感謝。
src_simple_recent_comments_by_id original source by:
http://www.g-loaded.eu/2006/01/15/simple-recent-comments-wordpress-plugin/
widget_simple_recent_comments_by_id original source by:
http://mika.kfib.org/wordpress/2006/04/07/widget-for-simple-recent-comments/
Thanks a bunch for sharing this with all people you really know what you’re talking approximately! Bookmarked. Please also discuss with my website =). We can have a link alternate arrangement among us
I see you don’t monetize your page, don’t waste your traffic, you
can earn extra cash every month because you’ve got high quality content.
If you want to know how to make extra bucks, search for: best adsense alternative Wrastain’s tools