秋雨De blog

  • 首页
  • 留言板
  • 关于
  • rss
秋雨De blog
一个技术小白的个人博客
  1. 首页
  2. wordpress开发
  3. 正文

为wordpress博客添加留言板

2019年12月29日 37222点热度 2人点赞 1条评论

wordpress本身并没有留言板共功能,需要我们自己使用插件或者新建留言模板的方式来更改。这里介绍的是新建留言模板。

首先我们要复制一个你的网站页面的模板,在wp-content/themes/你的主题名文件夹里面的single.php模板文件,将其复制一份,将其更名为guestbook.php。打开文件加入一段注释,来告诉wordpress这是一个模板文件

<?php
/*
Template Name: Guestbook
*/
?>

将其上传到到 wp-content/themes/你的主题名 文件夹里面,然后来到博客后台,新建一个页面,在页面属性选择新建的 guestbook 模板然后选择发布就能看到自己建立的留言板.

可以发现此时的留言板并不像一个留言板反而像一个文章页面,所以我们需要对其进行更改,由于每个模板文件结构不一样,所以关于文件位置只介绍大概,在新建的gusetbook.php里有一个get_template_part()带的参数是一个loop-post文件

	<?php get_template_part( 'content/loop', 'guset' ); ?>

我们找到文件位置,新建一个文件 命名为loop-guset.php,并对其进行更改,让他更适合留言板

在 loop-guset .php文件里有一个函数

<?php if ( comments_open() || '0' != get_comments_number() ) { comments_template(); } ?>

这里的comments_template可以带一个参数指定文件,默认的为comments.php文件,所以我们按部就班,将其赋值一个命名为guestcomments.php文件,并在函数comments_template()里对其调用

<?php if ( comments_open() || '0' != get_comments_number() ) { comments_template('/guestcomments.php'); } ?>

我们打开 guestcomments.php文件 我们只需要在后面对comment_form()函数对其赋值。

comment_form()函数官方文档对其参数解释:

原文档

Description #Description

Most strings and form fields may be controlled through the $args array passed into the function, while you may also choose to use the ‘comment_form_default_fields’ filter to modify the array of default fields if you’d just like to add a new one or remove a single field. All fields are also individually passed through a filter of the ‘comment_form_field_$name’ where $name is the key used in the array of fields.


Parameters #Parameters

$args

(array) (Optional) Default arguments and form fields to override.

  • 'fields'
    (array) Default comment fields, filterable by default via the 'comment_form_default_fields' hook.
    • 'author'
      (string) Comment author field HTML.
    • 'email'
      (string) Comment author email field HTML.
    • 'url'
      (string) Comment author URL field HTML.
    • 'cookies'
      (string) Comment cookie opt-in field HTML.
  • 'comment_field'
    (string) The comment textarea field HTML.
  • 'must_log_in'
    (string) HTML element for a 'must be logged in to comment' message.
  • 'logged_in_as'
    (string) HTML element for a 'logged in as [user]' message.
  • 'comment_notes_before'
    (string) HTML element for a message displayed before the comment fields if the user is not logged in. Default 'Your email address will not be published.'.
  • 'comment_notes_after'
    (string) HTML element for a message displayed after the textarea field.
  • 'action'
    (string) The comment form element action attribute. Default '/wp-comments-post.php'.
  • 'id_form'
    (string) The comment form element id attribute. Default 'commentform'.
  • 'id_submit'
    (string) The comment submit element id attribute. Default 'submit'.
  • 'class_form'
    (string) The comment form element class attribute. Default 'comment-form'.
  • 'class_submit'
    (string) The comment submit element class attribute. Default 'submit'.
  • 'name_submit'
    (string) The comment submit element name attribute. Default 'submit'.
  • 'title_reply'
    (string) The translatable 'reply' button label. Default 'Leave a Reply'.
  • 'title_reply_to'
    (string) The translatable 'reply-to' button label. Default 'Leave a Reply to %s', where %s is the author of the comment being replied to.
  • 'title_reply_before'
    (string) HTML displayed before the comment form title. Default: <h3 id="reply-title" class="comment-reply-title">.
  • 'title_reply_after'
    (string) HTML displayed after the comment form title. Default: </h3>.
  • 'cancel_reply_before'
    (string) HTML displayed before the cancel reply link.
  • 'cancel_reply_after'
    (string) HTML displayed after the cancel reply link.
  • 'cancel_reply_link'
    (string) The translatable 'cancel reply' button label. Default 'Cancel reply'.
  • 'label_submit'
    (string) The translatable 'submit' button label. Default 'Post a comment'.
  • 'submit_button'
    (string) HTML format for the Submit button. Default: <input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />.
  • 'submit_field'
    (string) HTML format for the markup surrounding the Submit button and comment hidden fields. Default: <p class="form-submit">%1$s %2$s</p>, where %1$s is the submit button markup and %2$s is the comment hidden fields.
  • 'format'
    (string) The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'.

Default value: array()$post_id

(int|WP_Post) (Optional) Post ID or WP_Post object to generate the form for. Default current post.

Default value: null

谷歌浏览器翻译

说明#说明

大多数字符串和表单字段可以通过传递到函数中的$args数组进行控制,同时如果你只是想添加一个新的字段或删除一个字段,你也可以选择使用'comment_form_default_fields'过滤器来修改默认字段数组。所有的字段也会单独通过'comment_form_field_$name'的过滤器,其中$name是字段数组中使用的键。


参数#参数

$ args

(array) (可选) 要覆盖的默认参数和表单字段。

  • “字段”
    (array) 默认评论字段,默认情况下可通过“ comment_form_default_fields”钩子进行过滤。
    • 'author'
      (string)评论作者字段HTML。
    • 'email'
      (string)评论作者电子邮件字段HTML。
    • 'url'
      (string)评论作者URL字段HTML。
    • “ cookies”
      (string)评论cookie选择加入字段HTML。
  • 'comment_field'
    (string)HTML注释文本框。
  • “ must_log_in”
    (string)用于 "以[用户]身份登录 "消息的HTML元素。
  • “ logged_in_as”
    (string)用于 "以[用户]身份登录 "消息的HTML元素。
  • 'comment_notes_before'
    (string)HTML元素,用于在用户未登录时,在评论栏前显示的信息。默认为'您的电子邮件地址不会被公布'。
  • “ comment_notes_after”(string)HTML元素,用于在textarea字段后显示信息。
  • 'action'
    (string) 评论表单元素id属性。默认为'commentform'。
  • 'id_submit'
    (string) 评论提交元素id属性。默认为'submit'。
  • 'id_form'
    (string)评论表单元素类属性。默认为'comment-form'。
  • 'id_submit'
    (string) 评论提交元素类属性。默认为'submit'。
  • 'class_form'
    (字符串)注释表单元素的类属性。默认为“评论形式”。
  • 'class_submit'
    (string) 评论提交元素名称属性。默认为'提交'。
  • 'name_submit'
    (string) 评论提交元素名称属性。默认为'提交'。
  • 'title_reply'
    (string) 可翻译的 "回复 "按钮标签。默认为'留下回复'。
  • 'title_reply_to'
    (string)可翻译的 "回复 "按钮标签,默认为 "给%s留下回复",其中%s是被回复的作者。默认为'Leave a Reply to %s',其中%s是被回复的评论的作者。
  • 'title_reply_before'
    (字符串)在评论表单标题前显示的HTML。默认值。<h3 id="reply-title" class="comment-reply-title">
  • “ title_reply_after”
    (字符串)在评论表单标题之后显示的HTML。默认值:</h3>。
  • “ cancel_reply_before”
    (字符串)在取消回复链接之前显示的HTML。
  • “ cancel_reply_after”
    (字符串)在评论表单标题后显示的HTML。默认值。</h3>
  • 'cancel_reply_link'
    (string) 可翻译的 "取消回复 "按钮标签。默认为'取消回复'。
  • 'label_submit'
    (string) 可翻译的 "提交 "按钮标签。默认为'发表评论'。
  • 'label_submit'
    (string) 可翻译的 "提交 "按钮标签。默认的 "发表评论"。
  • “ submit_button”
    (string) “提交”按钮的HTML格式。默认值:<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />。
  • “ submit_field”
    (字符串)提交按钮和注释隐藏字段周围的标记的HTML格式。默认值。<p class="form-submit">%1$s %2$s</p>其中%1$s为提交按钮标记,%2$s为评论隐藏字段。
  • 'format'
    (string) 注释格式。默认为'xhtml'。接受'xhtml','html5'。



    默认值:array()$post_id
    (int | WP_Post) (可选) 要为其生成表单的Post ID或WP_Post对象。默认为当前帖子。

默认值:null

由于我们需要做留言板,更改字段名,看起来是一个留言板,所以我们需要更改 label_submit,title_reply,comment_notes_after,comment_field这几个参数将其赋值并建立一个array,然后调用 comment_form() 函数,用以呈现我们留言板。

    $comments_args = array(
        // Change the title of send button
        'label_submit' => __( '发表留言', 'textdomain' ),
        // Change the title of the reply section
        'title_reply' => __( '留言板', 'textdomain' ),
        // Remove "Text or HTML to be displayed after the set of comment fields".
        'comment_notes_after' => '',
        // Redefine your own textarea (the comment body).
        'comment_field' => '<p class="comment-form-comment"><label for="comment">留言</label><br/><textarea id="comment" name="comment" aria-required="true" style="margin: 5px 0px; height: 254px; width: 522px;"></textarea></p>',
    );

官方文档 comment_form() https://developer.wordpress.org/reference/functions/comment_form/

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2019年12月29日

fallrain

种一棵树最好的时间是十年前,其次是现在。

点赞
< 上一篇
下一篇 >

文章评论

  • ukk

    可以啊我也去掉了。

    2022年8月7日
    回复
  • razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
    回复 ukk 取消回复

    fallrain

    种一棵树最好的时间是十年前,其次是现在。

    文章目录
    • Description #Description
      • Parameters #Parameters
    • 说明#说明
      • 参数#参数
    友情连接
    猫饭范文泉博客迎風别葉CODING手艺人ScarSu
    归档
    • 2025 年 5 月
    • 2025 年 4 月
    • 2025 年 3 月
    • 2024 年 12 月
    • 2024 年 10 月
    • 2024 年 5 月
    • 2023 年 2 月
    • 2022 年 11 月
    • 2022 年 3 月
    • 2021 年 12 月
    • 2021 年 8 月
    • 2021 年 5 月
    • 2021 年 4 月
    • 2021 年 3 月
    • 2020 年 12 月
    • 2020 年 11 月
    • 2020 年 8 月
    • 2020 年 5 月
    • 2019 年 12 月
    • 2019 年 3 月

    吉ICP备18007356号

    吉公网安备22020302000184号

    Theme Kratos Made By Seaton Jiang

    COPYRIGHT © 2025 秋雨De blog ALL RIGHTS RESERVED