admin管理员组文章数量:1122846
This may sound like an unusual request but I am try to display comments from several related posts in a single comment template.
For example I have several posts on the same topic (IDs 253, 724, 798
) and I want all comments from these posts to appear next in a continuous thread. So if I am viewing post ID 724
I can also see the comments from posts 253
and 798
.
Can I pass multiple post ID
s to the comments template to achieve this does this require a custom query before or after the comments template to show all the comments in a merged thread?
This may sound like an unusual request but I am try to display comments from several related posts in a single comment template.
For example I have several posts on the same topic (IDs 253, 724, 798
) and I want all comments from these posts to appear next in a continuous thread. So if I am viewing post ID 724
I can also see the comments from posts 253
and 798
.
Can I pass multiple post ID
s to the comments template to achieve this does this require a custom query before or after the comments template to show all the comments in a merged thread?
2 Answers
Reset to default 2you can get comments from each post by its id with
$comments253 = get_comments('post_id=253');
$comments724 = get_comments('post_id=724');
$comments798 = get_comments('post_id=798');
then merge ( array-merge ) and sort the array by date ( comment->comment_date being the key to the date value) if you want. then just
foreach($comments as $comment) :
echo($comment->comment_author . '<br />' . $comment->comment_content);
endforeach;
This is all very manual, and you might want to automatize the process, but that's probably an different matter.
Getting the comments for multiple posts is rather simple: Just replace comment_post_ID = YOUR_POST_ID
with an IN()
function.
function wpse_59687_multiple_comment_post_id_query_filter( $query )
{
$post_ids = array ( 149, 188, 151 );
if ( FALSE === strpos( $query, 'comment_post_ID = ' ) )
{
return $query; // not the query we want to filter
}
remove_filter( 'query', 'wpse_59687_multiple_comment_post_id_query_filter' );
$replacement = 'comment_post_ID IN(' . implode( ',', $post_ids ) . ')';
return preg_replace( '~comment_post_ID = \d+~', $replacement, $query );
}
Now you call this function just before you call comments_template()
:
add_filter( 'query', 'wpse_59687_multiple_comment_post_id_query_filter' );
What’s more difficult:
get_comments_number()
is wrong. You have to filter its value too.- Replies to a comment from another post are redirected to the URL of the other post, not the page where the commenter has written the comment. Fixing that will be not so trivial …
本文标签: Show comments from multiple post IDs in comment template
版权声明:本文标题:Show comments from multiple post IDs in comment template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736296716a1929857.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论