WordPress邊欄調用帶Gravatar頭像的最新迴響

本文目錄1帶Gravatar頭像的最新迴響

目前的主題一般都是在邊欄顯示最新迴響,如果你比較喜歡折騰主題,你可以試試下面的代碼,這個帶Gravatar頭像的最新迴響,是一直以來在用的,也不記得最初來自哪裡瞭。

wpdaxue.com-201212217

帶Gravatar頭像的最新迴響

1.在主題的 functions.php 的最後一個 ?> 前面添加下面的代碼:

1
2
3
4
5
6
7
8
9
10
11
//邊欄迴響 from wpdaxue.com
function h_comments($outer,$limit){
    global $wpdb;
    $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url,comment_author_email, SUBSTRING(comment_content,1,22) AS com_excerpt 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 user_id='0' AND comment_author != '$outer' ORDER BY comment_date_gmt DESC LIMIT $limit";
    $comments = $wpdb->get_results($sql);
    foreach ($comments as $comment) {
        $output .= '<li>'.get_avatar( $comment, 32,"",$comment->comment_author).' <p class="s_r"><a href="'. get_permalink($comment->ID) .'#comment-' . $comment->comment_ID . '" title="《'.$comment->post_title . '》上的迴響"><span class="s_name">'.strip_tags($comment->comment_author).':</span><span class="s_desc">'. strip_tags($comment->com_excerpt).'</span></a></p></li>';
    }
    $output = convert_smilies($output);
    echo $output;
}

//邊欄迴響 from wpdaxue.com
function h_comments($outer,$limit){
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url,comment_author_email, SUBSTRING(comment_content,1,22) AS com_excerpt 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 user_id=’0′ AND comment_author != ‘$outer’ ORDER BY comment_date_gmt DESC LIMIT $limit";
$comments = $wpdb->get_results($sql);
foreach ($comments as $comment) {
$output .= ‘<li>’.get_avatar( $comment, 32,"",$comment->comment_author).’ <p class="s_r"><a href="’. get_permalink($comment->ID) .’#comment-‘ . $comment->comment_ID . ‘" title="《’.$comment->post_title . ‘》上的迴響"><span class="s_name">’.strip_tags($comment->comment_author).’:</span><span class="s_desc">’. strip_tags($comment->com_excerpt).'</span></a></p></li>’;
}
$output = convert_smilies($output);
echo $output;
}

2.在主題的 sidebar.php 文件使用下面的調用代碼即可:

1
2
3
4
5
6
<h3>最新迴響</h3>
	<div id="comment-list">
		<ul>
		<?php h_comments($outer='博主',$limit='10'); ?>
		</ul>
	</div>

<h3>最新迴響</h3>
<div id="comment-list">
<ul>
<?php h_comments($outer=’博主’,$limit=’10’); ?>
</ul>
</div>

註:$outer 後面應填寫博主的昵稱,這樣可以使博主的迴響不在邊欄迴響中顯示;$limit 後面填寫的是調用數量,10表示 10條。

到這裡就基本搞定瞭,剩下的就是根據自己的實際來修改添加CSS樣式即可,下面的CSS僅供參考:

1
2
3
4
5
6
7
#comment-list{width:280px;}
#comment-list li{height:40px;border-bottom:1px dashed #eee;padding:8px 0}
#comment-list li a{display:inline;width:280px;height:40px;cursor:pointer}
#comment-list .avatar{float:left;width:32px;height:32px;border:1px dashed #eee;padding:2px}
#comment-list .s_r{float:left;margin-left:5px;height:40px}
#comment-list .s_name{display:block;overflow:hidden;width:222px;height:20px;text-overflow:ellipsis;white-space:nowrap;font-size:14px}
#comment-list .s_desc{display:block;overflow:hidden;width:222px;height:20px;color:#999;text-overflow:ellipsis;white-space:nowrap;line-height:20px}

#comment-list{width:280px;}
#comment-list li{height:40px;border-bottom:1px dashed #eee;padding:8px 0}
#comment-list li a{display:inline;width:280px;height:40px;cursor:pointer}
#comment-list .avatar{float:left;width:32px;height:32px;border:1px dashed #eee;padding:2px}
#comment-list .s_r{float:left;margin-left:5px;height:40px}
#comment-list .s_name{display:block;overflow:hidden;width:222px;height:20px;text-overflow:ellipsis;white-space:nowrap;font-size:14px}
#comment-list .s_desc{display:block;overflow:hidden;width:222px;height:20px;color:#999;text-overflow:ellipsis;white-space:nowrap;line-height:20px}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *