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

2023年5月27日18:00:41 发表评论

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

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

  1. <?php if ( is_author() ) { ?>
  2. <?php
  3. $author_email = get_the_author_meta( 'user_email' );
  4. $args = array(
  5.     'author_email' => $author_email,
  6.     'number' => '10'
  7. );
  8. $comments = get_comments($args);
  9. foreach($comments as $comment) :
  10.     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>';
  11. endforeach;
  12. ?>
  13. <?php } ?>

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

  1. <?php if ( is_author() ) { ?>
  2. <aside id="secondary" class="sidebar widget-area"  role="complementary">
  3. <section id="user-p" class="widget widget_categories">
  4.     <h2 class="widget-title">评论过的文章</h2>
  5.     <ul class="user-p-widget">
  6.         <?php
  7.         $author_email = get_the_author_meta( 'user_email' );
  8.         $args = array(
  9.             'author_email' => $author_email,
  10.             'number' => '10'
  11.         );
  12.         $comments = get_comments($args);
  13.         foreach($comments as $comment) :
  14.             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>';
  15.         endforeach;
  16.         ?>
  17.     </ul>
  18. </section>
  19. </aside>
  20. <?php } ?>

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

Tips:

做副业长期赚不到钱的话很容易放弃,特别是对于新手来说,新手刚入行的时候期望值都比较高,以为进来就马上能赚到钱,但是现实却很残酷,很多新手都没能坚持到赚钱的时候。因此,我在朋友圈《加微信》会给大家推荐一些破零项目,钱虽然不多,但是我觉得能让很多新手做副业的信心不那么容易被打垮。

历史上的今天:

掘金网
  • 版权声明:本站原创文章,转载请保留出处和链接!
  • 本文链接:http://jjsoho.com/fbgplwz/ 网赚有风险,投资需谨慎!