Wordpress 评论顺序错误,Gravatar 无法正常工作
Wordpress comments in the wrong order and gravatar not working
我是 WordPress 的新手,我正在尝试应用我的一些 OOP 知识,这让我陷入困境并寻求您的帮助
我创建了一条评论class:
<?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>
第一期:
结果是第一个 post 的评论出现在最后一个 post.
第二期:
头像显示在文本 "Author: "
旁边
到目前为止我只有一个评论,与第一个 post 有关,由 "A WordPress Commenter"
发表
如果我使用 comment_author(),则会显示 "Anonymous" - 用户不应该仍然显示匿名类型的头像吗?
如果我尝试 get_avatar() 而不是 wp_list_comments( array( 'avatar_size' => '16' ),那么我会收到以下错误:
Missing argument 1 for get_avatar(),
如何获取作者的 ID 以传递给 get_avatar?
提前致谢
想通了。您必须首先加载对象,然后从 while 循环中调用 getComment 函数。当 Whosebug 系统允许时,我会 select 几天后将其作为完整答案
这里是评论class:
<?php
class Comments {
public $commentSection;
public $commentPostId;
public $commentArr;
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 ) {
foreach ( $comments as $comment ) {
$this->commentArr = get_comment( get_the_author_meta('ID') );
$this->commentPostId = $this->commentArr->comment_post_ID;
// echo " comment post id: " . $this->commentPostId;
// echo " post id: " . get_the_ID();
if($this->commentPostId == get_the_ID()){
$this->commentSection = "
<article class='post'>
<header>
<h3> Comments</h3>
</header>
<p>
";
$this->commentSection .= 'Author: ' . get_avatar(get_comment_author_email(), $size = '16') . " comment id: " . $this->commentPostId;
$this->commentSection .= 'Date: ' . comment_date();
$this->commentSection .= 'Comment: ' . $comment->comment_content;
$this->commentSection .= "
</p>
</article>
";
}
}
echo $this->commentSection;
} else {
$this->commentSection = '';
}
}
}
$commentsObj = new Comments();
以下是部分索引页:
<?php
require_once('includes/comments.inc.php');
?>
<?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
// had to add this for home page
if( get_comment( get_the_author_meta('ID') )->comment_post_ID == get_the_ID() ) {
$commentsObj->getComments();
}
?>
<?php
}
}
?>
</div>
我是 WordPress 的新手,我正在尝试应用我的一些 OOP 知识,这让我陷入困境并寻求您的帮助
我创建了一条评论class:
<?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>
第一期: 结果是第一个 post 的评论出现在最后一个 post.
第二期: 头像显示在文本 "Author: "
旁边到目前为止我只有一个评论,与第一个 post 有关,由 "A WordPress Commenter"
发表如果我使用 comment_author(),则会显示 "Anonymous" - 用户不应该仍然显示匿名类型的头像吗?
如果我尝试 get_avatar() 而不是 wp_list_comments( array( 'avatar_size' => '16' ),那么我会收到以下错误:
Missing argument 1 for get_avatar(),
如何获取作者的 ID 以传递给 get_avatar?
提前致谢
想通了。您必须首先加载对象,然后从 while 循环中调用 getComment 函数。当 Whosebug 系统允许时,我会 select 几天后将其作为完整答案
这里是评论class:
<?php
class Comments {
public $commentSection;
public $commentPostId;
public $commentArr;
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 ) {
foreach ( $comments as $comment ) {
$this->commentArr = get_comment( get_the_author_meta('ID') );
$this->commentPostId = $this->commentArr->comment_post_ID;
// echo " comment post id: " . $this->commentPostId;
// echo " post id: " . get_the_ID();
if($this->commentPostId == get_the_ID()){
$this->commentSection = "
<article class='post'>
<header>
<h3> Comments</h3>
</header>
<p>
";
$this->commentSection .= 'Author: ' . get_avatar(get_comment_author_email(), $size = '16') . " comment id: " . $this->commentPostId;
$this->commentSection .= 'Date: ' . comment_date();
$this->commentSection .= 'Comment: ' . $comment->comment_content;
$this->commentSection .= "
</p>
</article>
";
}
}
echo $this->commentSection;
} else {
$this->commentSection = '';
}
}
}
$commentsObj = new Comments();
以下是部分索引页:
<?php
require_once('includes/comments.inc.php');
?>
<?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
// had to add this for home page
if( get_comment( get_the_author_meta('ID') )->comment_post_ID == get_the_ID() ) {
$commentsObj->getComments();
}
?>
<?php
}
}
?>
</div>