【发布时间】:2018-10-09 23:03:42
【问题描述】:
我是 WordPress 新手,我正在尝试应用我的一些 OOP 知识,这导致我陷入困境并寻求您的帮助
我创建了一个 cmets 类:
<?php
class Comments {
public $commentSection;
public function getComments() {
//Get only the approved comments
$args = array(
'status' => 'approve'
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
if ( $comments ) {
$this->commentSection = "
<article class='post'>
<header>
<h3> Comments</h3>
</header>
<p>
";
foreach ( $comments as $comment ) {
$this->commentSection .= 'Author: ' . wp_list_comments( array( 'avatar_size' => '16' ) );
$this->commentSection .= 'Date: ' . comment_date();
$this->commentSection .= 'Comment: ' . $comment->comment_content;
}
$this->commentSection .= "
</p>
</article>
";
} else {
$this->commentSection = '';
}
echo $this->commentSection;
}
}
$commentsObj = new Comments();
$commentsObj->getComments();
以下是我的 index.php 页面的一部分:
<section>
<div class="container">
<?php
if(have_posts()){
while(have_posts()){
the_post();
?>
<article class="post">
<header>
<a href=" <?php the_permalink(); ?> " target='_self'><h1> <?php the_title(); ?> </h1></a>
</header>
<p>
<?php the_content(); ?>
</p>
</article>
<?php
require_once('includes/comments.inc.php');
?>
<?php
}
}
?>
</div>
第一期: 结果是第一篇文章的 cmets 出现在最后一篇文章中。
第二期: 头像显示在文本“作者:”旁边
目前我只有一条评论,与“A WordPress Commenter”发表的第一篇文章有关
如果我使用comment_author(),那么会显示“匿名”——该用户不应该还有匿名类型的头像要显示吗?
如果我尝试 get_avatar() 而不是 wp_list_cmets(array( 'avatar_size' => '16' ),则会收到以下错误:
Missing argument 1 for get_avatar(),
如何让作者的 id 传递给 get_avatar?
提前致谢
【问题讨论】:
-
我能够通过使用以下命令找出 get_avatar 是 - get_avatar(get_comment_author_email(), $size = '16')