WordPress侧边栏调用带Gravatar头像的最新评论的方法

我本人最近迷上了WordPress,在浏览他人的站点时,看到侧边栏显示最新评论的效果,很是喜欢。但是本人代码又不会写,只能是在别人代码基础上进行修改(博主我是爱瞎折腾性的)。网上找了好久,终于给找到了两种实现方法,下面就说说如何使用。

方法一、
1、在主题的 functions.php 的最后一个 ?> 前面添加下面的代码:

//边栏评论
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 文件使用下面的调用代码即可:

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

注:$outer 后面应填写博主的昵称,这样可以使博主的评论不在边栏评论中显示;$limit 后面填写的是调用数量,10表示10条。

到这里就基本搞定了,剩下的就是根据自己的主题来修改CSS样式即可。

下面的CSS可根据自己网站样式进行修改:

#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}

方法二、
1、新建一个r_comment.php,并复制下列代码:

<?php if (get_option('swt_type') == 'Display') { ?>
<div class="r_comment">
<ul>
    <?php
        global $wpdb;
        $my_email = get_bloginfo ('admin_email');
        $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,50) 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 comment_author_email != '$my_email' ORDER BY comment_date_gmt DESC LIMIT 10";
        $comments = $wpdb->get_results($sql);
        $output = $pre_HTML;
        foreach ($comments as $comment) {
        $a= get_bloginfo('wpurl') .'/avatar/'.md5(strtolower($comment->comment_author_email)).'.jpg';
        $output .= "\n<li><img src='". $a ."'  alt=\"$comment->comment_author\" class='avatar'/>$comment->comment_author:<br /><a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"查看: " .$comment->post_title . "\">" . strip_tags($comment->com_excerpt)."</a></li>";
        }
        $output .= $post_HTML;
        $output = convert_smilies($output);
        echo $output;
    ?> 
</ul>
</div>
    <?php { echo ''; } ?>
        <?php } else { include(TEMPLATEPATH . '/includes/r_comment2.php'); } ?>
 

2、再新建一个r_comment2.php,然后复制下列代码并保存:

<div class="r_comment">
<ul>
        <?php
            global $wpdb;
            $my_email = get_bloginfo ('admin_email');
            $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,50) 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 comment_author_email != '$my_email' ORDER BY comment_date_gmt DESC LIMIT 10";
            $comments = $wpdb->get_results($sql);
            $output = $pre_HTML;
            foreach ($comments as $comment) {$output .= "\n<li>".get_avatar(get_comment_author_email(), 40).strip_tags($comment->comment_author).":<br />" . " <a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"查看: " .$comment->post_title . "\">" . strip_tags($comment->com_excerpt)."</a></li>";}
            $output .= $post_HTML;
            $output = convert_smilies($output);
            echo $output;
        ?> 
</ul>
</div>
 

3、在当前主题目录下新建一个includes的文件夹,并将r_comment.php和r_comment2.php上传至该文件夹内。
4、在主题的sidebar.php 文件使用下面的调用代码即可:

<h3>最新评论</h3>
<?php include('includes/r_comment.php'); ?>
 

5、到此基本上搞定了,下面附上该CSS代码,可根据自己网站情况进行修改:

.r_comment {}
.r_comment ul {list-style-type:none;}
.r_comment li {font-family:Microsoft YaHei;line-height:24px; clear:both;height:auto; overflow:hidden;color:#5bc0eb;font-size:14px}
.r_comment ul li img.avatar {float:left;padding:3px;margin:3px 4px 0 0;background:#fff;border: 1px solid #ddd;border-radius:50%;width:40px;height:40px;}
.r_comment_author{font-size:14px;margin:0;padding:0;color:#5bc0eb}
.r_comment a{font-family:Microsoft YaHei;font-size:16px;}
 

好了,侧边栏实现最新评论显示的两种方法就介绍完了,上面两种方法我都试过,可以实现。其中方法一取自Wordpress大学的《WordPress边栏调用带Gravatar头像的最新评论》,在此表示感谢!!方法二,本人再次找的话,又找不见了,如日后本人再次看到时,一定附上链接。

赞(0)
未经允许不得转载:伊阳博客 » WordPress侧边栏调用带Gravatar头像的最新评论的方法
所属分类: WordPress

评论 1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

表情 关闭

图片
目前评论:1 条  其中:访客  1 条  站长  0 条
  1.  沙发
    Fatansy
    Fatansy 【农民】(  Google Chrome 64.0.3282.204   Windows 10 ) 来自: 安徽省合肥市 移动
    发表于1年前 (2019-01-08)回复

    感谢博主分享,这个正是我想要的,经过测试完全可以试,棒棒的 [赞]