首页 视频课程 主题开发课程第07章、文章评论 WordPress给文章插入评论的方法

WordPress给文章插入评论的方法

2023-06-11 / 232阅

在WordPress中,可以通过在文章中嵌入评论框来允许读者对文章进行评论。以下是在WordPress文章中插入评论的方法和示例代码:

  1. 在WordPress后台,进入“外观”->“编辑器”,打开主题的functions.php文件;
  2. 在functions.php文件中添加以下代码:
// 在文章末尾插入评论框
function add_comment_form_to_posts( $content ) {
if ( is_single() ) {
ob_start();
comment_form();
$comments = ob_get_clean();
$content .= $comments;
}
return $content;
}
add_filter( 'the_content', 'add_comment_form_to_posts' ); 
  1. 保存文件并刷新文章页面,你会在文章末尾看到一个评论框。

注意:如果你使用的是Markdown格式,可以在文章模板的相应位置插入以下代码来添加评论框:

<!-- wp:comments -->
<comments-area></comments-area>
<!-- /wp:comments --> 

阅读文章或者观看视频过程中有任何问题,请下方留言或者联系我Q248758228

大家谈论
    我的见解
    目录