实现wordpress首页文章列表中显示文章评论

WordPress教程 5405

不少瀑布流型网站在首页显示文章的同时会调用该文章最近的网友评论,意思即是在首页每篇日志下像文章页显示评论,直接使用文章评论调用代码似乎是实现不了,下面是露兜博客给出的解决方案,实现或操作起来相当简单容易。

实现方法:

在wordpress模板的index.php文件中的文章调用循环内,while (have_posts()) : the_post();和endwhile;之间适当位置,添加以下代码:

1
2
3
4
5
6
<?php
    global $withcomments;
    $withcomments = true;
    // 包含评论模板文件,
    comments_template("/inline-comments.php");
?>

示例:

1
2
3
<?php while (have_posts()) : the_post(); ?>
	<?php global $withcomments; $withcomments = true; comments_template("/opinion.php");?>
<?php endwhile; ?>

实现原理:

使用全局变量$withcomments,并将其值改成true(改成这样也是可以的$withcomments = 1;)。接着包含用于在首页显示评论的模板opinion.php,如想使用默认评论模板comments.php,改成comments_template();

该方法可用于分类页、标签页、日期归档页等!

代码出自露兜博客,转载请注明!

精品推荐: