我有single.php文件,我m showing single post in it. I want to show comments on related to this post under this post. But the problem is I
正在评论任何帖子下面的所有评论。
以下是我的single.php代码
<?php
/*
* The template for displaying all single posts and attachments
*/
?>
<?php get_header(); ?>
<div id="single_post_wrap">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="time_and_author"><?php the_time('F jS, Y') ?> by <?php the_author() ?></div>
<div class="post_content"><?php the_content(); ?></div>
<p>Posted in <?php the_category(', ') ?> | <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
<?php endwhile; endif; ?>
<?php foreach (get_comments() as $comment): ?>
<div><?php echo $comment->comment_author; ?> said: "<?php echo $comment->comment_content; ?>".</div>
<?php endforeach; ?>
<?php comments_template(); ?>
</div>
</div>
<?php get_footer(); ?>
以下是我的comments.php代码
<?php $comment_args = array(
'comment_notes_after' => '',
'title_reply' => 'Have something to say?'
) ?>
提前致谢。
答案 0 :(得分:2)
使用is_single()查看帖子:
<?php if( is_single() ) : ?>
<?php foreach (get_comments() as $comment): ?>
<div><?php echo $comment->comment_author; ?> said: "<?php echo $comment->comment_content; ?>".</div>
<?php endforeach; ?>
<?php comments_template(); ?>
<?php endif; // close to check single.php ?>
答案 1 :(得分:0)
从 single.php :
中删除foreach
<?php get_header(); ?>
<div id="single_post_wrap">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="time_and_author"><?php the_time('F jS, Y') ?> by <?php the_author() ?></div>
<div class="post_content"><?php the_content(); ?></div>
<p>Posted in <?php the_category(', ') ?> | <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
<?php comments_template(); ?>
<?php endwhile; endif; ?>
</div>
</div>
<?php get_footer(); ?>
然后,将其添加到 comments.php 并在$comment_args
中传递get_comments()
,如下所示:
<?php $comment_args = array(
'comment_notes_after' => '',
'title_reply' => 'Have something to say?'
)
?>
<?php foreach (get_comments($comment_args) as $comment): ?>
<div>
<?php echo $comment->comment_author; ?> said: "<?php echo $comment->comment_content; ?>".
</div>
<?php endforeach; ?>
查看get_comments
的{{3}},查看可以传递到get_comments
的参数列表
答案 2 :(得分:0)
Single.php 代码。删除get_comments()
<?php
/*
* The template for displaying all single posts and attachments
*/
?>
<?php get_header(); ?>
<div id="single_post_wrap">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="time_and_author"><?php the_time('F jS, Y') ?> by <?php the_author() ?></div>
<div class="post_content"><?php the_content(); ?></div>
<p>Posted in <?php the_category(', ') ?> | <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
<?php endwhile; endif; ?>
<?php foreach ($comments as $comment): ?>
<div><?php echo $comment->comment_author; ?> said: "<?php echo $comment->comment_content; ?>".</div>
<?php endforeach; ?>
<?php comments_template(); ?>
</div>
</div>
<?php get_footer(); ?>
您需要为每页评论get_the_ID()
,然后添加评论查询。
$id - get_the_ID();
<?php $comment_args = array(
'comment_notes_after' => '',
'title_reply' => 'Have something to say?',
'post__in' => $id, //Retrieves comments for an array of posts
'post_id' => $post_ID // Post's ID you can make sure only comments related to that post appear.
)
?>
// The comment query
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );