WordPress显示文章作者最近发表过评论的文章
将代码加到侧边栏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 } ?>
其它主题需要查看主题侧边栏小工具结构,修改其中的结构代码。