首页 > 解决方案 > 如何为 WordPress 自定义 PHP 模板上的特定帖子调用 comment_form()

问题描述

我正在修改一个模板文件,该文件循环遍历许多类似于 WordPress 中的存档页面的帖子。在该输出上,我可以显示已使用 get_comments 和 wp_list_comments 提交的评论。但是,我无法终生获得要输出的评论表。我已经尝试了许多不同种类的comment_form() 与args 提交page_id 但无济于事我要么打破页面,要么它只是没有出现。评论绝对是开放的,评论是可能的,但只是没有显示在页面上。

comment_form($featuredposts[$articlePageNo]->ID);

不会破坏我的页面,但会以没有形式返回。

comment_form(post_id => $featuredposts[$articlePageNo]->ID);

打破页面。我认为这可能是由于防止在 WordPress 中的非单个页面上显示 comment_form,但我似乎无法隔离如何告诉 WordPress 可以在我的页面上执行此操作。

comment_form($featuredposts[$articlePageNo]->ID); //ID is the post ID from my loop.

//This works for displaying the already submitted comments:
//Gather comments for a specific page/post
$comments = get_comments(array(
      'post_id' => $postIDD,
      'status' => 'approve' //Change this to the type of comments to be displayed
       ));
//Display the list of comments
wp_list_comments(array(
             'per_page' => 10, //Allow comment pagination
             'reverse_top_level' => false //Show the latest comments at the 
top of the list
             ), $comments);

标签: phpwordpress

解决方案


comment_form()实际上将 post id 作为第二个参数,如Developer Documentation中所述。(如果您不提供帖子 ID,则使用当前页面的帖子 ID)

所以尝试将 ID 作为第二个参数传递,如下所示:

comment_form(null, $featuredposts[$articlePageNo]->ID);

推荐阅读