Php Wordpress独立评论页-带分页
我想建立一个网页,将显示所有的评论,无论他们附加到哪个职位。我还希望该页面被分页,因为它可能有10000多条评论Php Wordpress独立评论页-带分页,php,pagination,comments,wordpress,Php,Pagination,Comments,Wordpress,我想建立一个网页,将显示所有的评论,无论他们附加到哪个职位。我还希望该页面被分页,因为它可能有10000多条评论 我不知道该怎么做,但以下是我到目前为止研究过的一些函数: -如果没有传入post\u id,它将返回所有注释。但是,我看不到一种分页的方法(有offset和number选项可供摆弄,但手动操作非常繁琐) -关于这一点的文档非常糟糕,但建议如果与get_comments一起使用,我们可以循环使用所有注释,方法是将get_comments数组作为第二个参数传递。然而,这仍然会使用get
我不知道该怎么做,但以下是我到目前为止研究过的一些函数:
post\u id
,它将返回所有注释。但是,我看不到一种分页的方法(有offset
和number
选项可供摆弄,但手动操作非常繁琐)get_comments
一起使用,我们可以循环使用所有注释,方法是将get_comments
数组作为第二个参数传递。然而,这仍然会使用get_comments
来实际。。。好吧,看看评论,似乎没有办法把它编成页码wp\u list\u注释一起使用(没有第二个参数)
wp\u list\u注释
(没有第二个参数)我所尝试的:
get\u comments
中的number
参数:
$comments = get_comments(array(
'status' => 'approve',
'number' => '2'
));
wp_list_comments(array(
'callback' => 'my_rendering_function'
), $comments);
paginate_comments_links();
这不会显示任何分页链接get_comments
将所有注释加载到内存中时也会畏缩问题: 如何对所有评论进行分页
p.S.我正在使用WordPress 3.4.1和PHP 5.3.2。如果您计划构建自己的分页,您需要知道您将要使用的注释总数,因此您必须加载所有注释 我已经建立了以下我将使用,让我知道,如果它有帮助
#Config here.
define('DEFAULT_COMMENTS_PER_PAGE',100);
$page = (int) (!isset($_REQUEST["page"]) ? 1 : $_REQUEST["page"]);
$limit = DEFAULT_COMMENTS_PER_PAGE;
$offset = ($page * $limit) - $limit;
$param = array(
'status'=>'approve',
'offset'=>$offset,
'number'=>$limit,
);
$total_comments = get_comments(array('status'=>'approve'));
$pages = ceil(count($total_comments)/DEFAULT_COMMENTS_PER_PAGE);
$comments = get_comments($param);
foreach($comments as $comment) {
// ECHO THE AUTHOR AND COMMENT
echo("<strong>{$comment->comment_author}</strong> - {$comment->comment_content}");
}
$args = array(
'base' => 'https://example.com/all-comments/%_%',
'format' => '?page=%#%',
'total' => $pages,
'current' => $page,
'show_all' => False,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => True,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'type' => 'plain');
// ECHO THE PAGENATION
echo paginate_links( $args );
#在此处进行配置。
定义(“每页默认注释”,100);
$page=(int)(!isset($_请求[“页面])?1:$_请求[“页面]);
$limit=每页的默认注释;
$offset=($page*$limit)-$limit;
$param=数组(
“状态”=>“批准”,
“偏移量”=>$offset,
“编号”=>$limit,
);
$total_comments=get_comments(数组('status'=>'approve');
$pages=ceil(计数($total_comments)/每页默认_comments_);
$comments=get_comments($param);
foreach($comments作为$comment){
//回应作者和评论
echo({$comment->comment\u author}-{$comment->comment\u content});
}
$args=数组(
'基本'=>'https://example.com/all-comments/%_%',
'格式'=>'?页面=%#%',
“总计”=>$pages,
“当前”=>$page,
“全部显示”=>False,
“结束大小”=>1,
“中等尺寸”=>2,
“上一步”=>正确,
“上一个文本”=>“(上一个”),
“下一个文本”=>“‘下一个’”,
'类型'=>'普通');
//呼应
回显分页链接($args);
我们应该担心使用get_comments()获取总注释的性能。必须将参数offset用作false,并将count用作true,如下所示:
$comments_per_page = 2;
$page = 2;
//MYSQL: LIMIT offset, number
$params = array(
'post_id' => $post_id,
'offset' => (--$page) * $comments_per_page,
'number' => $comments_per_page,
);
$comments = get_comments( $params );
$total_comments = get_comments(
array_merge($params,
array(
'count' => true,
'offset' => 0,
'number' => 0
)
)
);
$args = array(
'total' => ceil( $total_comments / $comments_per_page ),
'current' => max( 1, $page ),
);
$pagination = paginate_links( $args );
添加您需要的任何参数。您可以使用wp_list_comments()作为weel,但如果您想自定义输出HTML,请使用foreach for$comments:
foreach ( $comments as $comment ) :
echo $comment->comment_date;
echo '<br>';
echo $comment->comment_author;
echo '<br>';
echo $comment->comment_content;
endforeach;
foreach($comments as$comment):
echo$comment->comment\u date;
回声“
”;
echo$comment->comment\u作者;
回声“
”;
echo$comment->comment\u内容;
endforeach;
我看不到一个直接的方法来实现这一点,你最终需要使用偏移量
和数字
来获得所需的注释来显示。看看SEO寻呼机插件,你可以获得分页功能,或者使用自定义WordPress循环来实现你的功能。@Umeshawashi-你读过吗?看来应该有办法做到这一点。我是不是误解了什么?只是为了改进我之前的回答,你不需要实际获取所有评论,但你至少应该知道你所在位置前面的x。在一个页面上,你可能想显示像1,2,3,4,。。。所以你需要知道你是否有足够的评论来填满这些页面,或者你是否必须早点停下来。只需获得4x25就可以帮助您找到这些信息。如果你想知道总页数,你仍然需要知道评论的总数。这段代码可以工作,但在第14行@PsyKzz可能是有意的:$comments=get_comments($param)
在第20行中,您可能还需要填写分页功能的基本URL,例如'base'=>'https://example.com/all-comments/%_%“
。有关详细信息,请参阅用于分页链接()的。除非我这样做,否则分页对我不起作用。
foreach ( $comments as $comment ) :
echo $comment->comment_date;
echo '<br>';
echo $comment->comment_author;
echo '<br>';
echo $comment->comment_content;
endforeach;