WordPress 显示文章作者最近发表过评论的文章

这段代码用于在WordPress作者页面侧边栏显示该作者最近评论过的10篇文章。通过获取作者邮箱匹配评论记录,生成包含文章标题、链接和评论日期的列表。代码适配了Twenty Sixteen主题的样式结构,其他主题需根据侧边栏小工具结构相应调整HTML标签。

WordPress基础教程
阅读时间: 27 分钟
最后更新时间:2025年11月13日

将代码加到侧边栏sidebar.php模板中,当打开文章作者所有文章归档页面,会显示10篇该作者最近发表过评论的文章。

<?php if ( is_author() ) { ?>
<?php
$author_email = get_the_author_meta( 'user_email' );
$args = array(
    'author_email' => $author_email,
    'number' => '10'
);
$comments = get_comments($args);
foreach($comments as $comment) :
    echo('<li class="comment">' . $somment->comment_content),'<h5><a href='.get_permalink($comment->comment_post_ID).'>', get_the_title($comment->comment_post_ID), '</a></h5>', '<time><em>' . $comment->get_comment_date . '</em></time>', '</li>';
endforeach;
?>
<?php } ?>

当然如果想让上述代码能适配主题样式结构,还需要加上适当的DIV,比如适配WordPress默认主题Twenty Sixteen,代码应该是这样:

<?php if ( is_author() ) { ?>
<aside id="secondary" class="sidebar widget-area"  role="complementary">
<section id="user-p" class="widget widget_categories">
    <h2 class="widget-title">评论过的文章</h2>
    <ul class="user-p-widget">
        <?php
        $author_email = get_the_author_meta( 'user_email' );
        $args = array(
            'author_email' => $author_email,
            'number' => '10'
        );
        $comments = get_comments($args);
        foreach($comments as $comment) :
            echo('<li class="comment">' . $somment->comment_content),'<a href='.get_permalink($comment->comment_post_ID).'>', get_the_title($comment->comment_post_ID), '</a>', '<time><em>' . $comment->get_comment_date . '</em></time>', '</li>';
        endforeach;
        ?>
    </ul>
</section>
</aside>
<?php } ?>

其它主题需要查看主题侧边栏小工具结构,修改其中的结构代码。

这篇文章有用吗?

点击星号为它评分!

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

曾凤祥

曾凤祥

技术负责人
小兽wordpress凭借多年的wordpress企业主题制作经验,坚持以“为用户而生的wordpress主题”为宗旨,累计为10万多家客户提供品质wordpress建站服务,得到了客户的一致好评。我们一直用心对待每一个客户,我们坚信:“善待客户,将会成为终身客户”。小兽wordpress能坚持多年,是因为我们一直诚信。

相关文章

如何让线上业务更上一层楼

还没有WordPress网站

还没有WordPress网站

不管你从事什么行业,WordPress都会为你提供一个专业的主题模板。在WordPress市场上有成千上万的免费主题,适合很多中小企业。

查看所有模板
已经有WordPress网站

已经有WordPress网站

小兽WordPress诚邀你一起学习WordPress,愿与各方携手升级改善您的WordPress网站,一起交流网站加速,网站优化等问题。

马上交个朋友
微信联系
chat 扫码联系
模板建站
挑选模板
网站定制
免费诊断
咨询热线
咨询热线

189-0733-7671

返回顶部