wordpress首页列表显示每篇的评论头像

只看标题也许还未明白,我要实现的是,在首页的文章列表里,每篇文章显示指定数量的评论者gravatar头像,就是这么蛋疼的效果,在搜索里无法找到实现的方法,虽然有一两篇文章有提到方法,但是仍然不能实现只显示指定数量的头像效果。

曾实在想不出方法,试图修改免插件的评论排行版代码,结果显然是失败了,事实证明,我是多么的天真无知,就因为我不懂代码,最后找到了其他方法,百般尝试以后,被我折腾出效果来,可想而知,我对问题凭着一定要达到目的的精神,再次验证了那句话,只要努力,就一定会成功。

wordpress列表里显示评论

方法:

在WordPress主题的index.php文件的文章调用循环内,即while (have_posts()) : the_post();endwhile;之间适当位置,添加以下代码:

<?php
global $withcomments;
$withcomments = true;
comments_template("/index-comments.php");  //调用模版
?>

如果你不用模版:上面的代码可以直接用:comments_template(); 但是这种方法跟文章页面调用评论没什么两样,最后会在首页显示一大堆评论信息和评论发表框,如何让它只显示评论者的gravatar头像,并且是指定数量,我想懂代码的一定很快就能折腾出来,但是不懂的,得尝试N次才能试出效果来。

我的模版是这样的:(另外还需要配合css才能控制显示效果)

<?php // Do not delete these lines
if ('comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');

if (!empty($post->post_password)) { // if there's a password
if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) {  // and it doesn't match the cookie
?>

<p class="nocomments">This post is password protected. Enter the password to view comments.</p>

<?php
return;
}
}

/* This variable is for alternating comment background */
$oddcomment = 'class="alt" ';
?>

<!-- You can start editing here. -->

<?php if ($comments) : ?>
<ol class="commentlist">
<ul class="comments-authors">
<li class="comments-avatar1">
<!--<div style="margin-top:3px; height:10px;"></div>-->
<?php foreach ($comments as $comment) : ?>
<a href="#" title="<?php comment_text() ?>"><?php if(function_exists('get_avatar')) { echo get_avatar($comment, '40'); } ?></a>
<!--
<h4 class="commentauthor">
<?php comment_author_link() ?>:
</h4>
<?php if ($comment->comment_approved == '0') : ?>
<em>Your comment is awaiting moderation.</em>
<?php endif; ?><?php comment_text() ?><span style="float:left;"></span> -->

<?php
/* Changes every other comment to a different class */
$oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
?>

<?php endforeach; /* end for each comment */ ?>

</ol></li></ul>

<?php else : // this is displayed if there are no comments so far ?>

<?php if ('open' == $post->comment_status) : ?>
<!-- If comments are open, but there are no comments. -->
<div class="post-content">
<div class="comment-counter">
<?php comments_popup_link(__( '0 Comments' ), __( '1 Comment' ), __( '% Comments' )); ?>
</div>
</div>
<ol class="commentlist" style="margin-bottom:0;">
<li style="min-height:77px;">
<h4 class="commentauthor">
No comments so far. Share your thoughts!
</h4>
</li>
</ol>

<?php else : // comments are closed ?>
<!-- If comments are closed. -->
<p class="nocomments">Comments are closed.</p>

<?php endif; ?>
<?php endif; ?>

如果有闲的慌的,可以玩玩,我已经把这个效果去掉了,感觉页面太大了,加载比较慢,不过你如果是一个喜欢折腾,并且喜欢有个性的,不妨也像这个站点一样,看看功能是否很给力,具体效果可以看看:www.takeasy.info 我感觉这个站点的主题很不错,作者玩的很有个性很出色,毕竟gae空间就是拿来玩的。

分类:网络博客 标签:,,

上一篇:令人蛋疼的西游记点解

下一篇:wordpress首页显示最新文章的评论

已经有57条评论!

Loading...
  1. 来过了,不错哦

    31F
  2. 调用这个感觉不太好看

    32F
  3. 主题很复杂,不想去修改!

    33F
  4. 楼主,你居然用代码实现了这个显示头像的问题,PHP技术不错啊 呵呵

    34F
  5. 你太厉害了,这个效果很不错。我时代码盲,你能抽空告诉我你的最新评论的格式是怎样的吗?

    35F
  6. 写的还真不错,以后有机会会场来的,希望能回访下。

    36F
  7. 行云流水!一气呵成,好文采~!

    37F
  8. 其实我对这个的看法,有点与博主不同。

    38F
  9. 这文章标题写得很有水平,赞!

    39F
  10. 相见恨晚啊,咦?~我好像也没见过博主。用错词了,呵呵

    40F
  11. 太多赞美的话我就不说了,博客很对我口味,已经收藏到收藏夹了!

    41F
  12. 要我自己写代码 会要了我小命!

    42F
  13. 努力就会成功

    43F
  14. 长知识了http://www.jejy.cn/

    44F

Leave a Reply to www.kanca.cn

gravatar

冷笑 忧伤 恶魔 笑脸 红脸 咧嘴笑 吃惊 惊讶 困惑 耍酷
大笑 抓狂 痛苦 转眼珠 眨眼 好主意 囧 无表情 哭泣 坏笑