5ucms论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 9816|回复: 0
打印 上一主题 下一主题

[钩子/函数] comment_form()函数 生成一个评论表单

[复制链接]

670

主题

785

帖子

8343

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
8343
跳转到指定楼层
楼主
发表于 2021-6-1 10:44:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
初识这个函数是因为做的一个企业主题需要添加一个自定义的留言表单,在使用的过程中遇见个不是问题的问题,就是每次打开有留言表单的页面,只出现一个填写评论内容的,默认的姓名、电子邮件和网址都没出现,刚开始还以为自己用错了,最后发现只要退出管理员账号,留言表单就能正常显示姓名、电子邮件、网址和评论所有表了,也是醉了。

在 WordPress 主题中, 使用 comment_form()函数来生成一个评论表单。通常把评论模块的代码写进单独的 comments.php 文件中,然后使用 comments_template ()函数在 single.php、page.php 等文件底部引用评论模块。

语法结构

  1. <?php comment_form( $args, $post_id ); ?>
复制代码

参数

$args (array) (optional) 包括$fields、标题和发送等的信息

$fields

$fields(array) (optional) 控制表单信息,包括
姓名 – author
邮箱 – email
网址 – url
留言内容 – comment_field
comment_notes_before – 在评论表单前面显示提示信息
comment_notes_after – 在评论表单后面显示提示信息

$args

title_reply – 改变评论表单标题,默认是:Leave a Reply。
title_reply_to
comment_notes_before – 在评论表单前面显示提示信息
comment_notes_after – 在评论表单后面显示提示信息
title_reply_before 给评论表单标题加上HTML结构或text,比如加个<h3>开头
title_reply_after 给评论表单标题加上HTML结构或text,比如加个</h3>结尾
cancel_reply_before
cancel_reply_after
cancel_reply_link
must_log_in
logged_in_as
comment_field 为false时默认的内容表单将不显示,默认是true
id_form 控制HTML结构中<form>的id值,默认是commentform
class_form 控制HTML结构中<form>的class值,默认是comment-form
id_submit 控制HTML结构中<input type=”submit”>的id值,默认是submit
class_submit 控制HTML结构中<input type=”submit”>的class值,默认是submit
label_submit – 这个参数改变评论表单提交按钮文字,默认是:Post Comment

更多详细请看

TIPS

要注意的是,如果你的主题是要给别人用的,特别是外国人,为了国际化,修改的内容要用 __() 这个函数包裹,可以方便翻译

实例

  1. <?php
  2. $commenter = wp_get_current_commenter();
  3. $req = get_option( 'require_name_email' );
  4. $aria_req = ( $req ? " aria-required='true'" : '' );
  5. $fields =  array(
  6.     'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . '</p>',
  7.     'author' => '<p><input id="author" placeholder="Name*" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="40"' . $aria_req . ' /></p>',
  8.     'email'  => '<p><input id="email" placeholder="Email*" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="40"' . $aria_req . ' /></p>',
  9.     'comment_field' => '<p></label><textarea id="comment" placeholder="Message*" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
  10. );
  11. $args = array(
  12.     'fields' =>  $fields,
  13.     'title_reply'=>'Contact Form',
  14.     'label_submit' => 'Send Message',
  15.     'comment_field' => false,
  16.     'comment_notes_before' => false
  17. );
复制代码

comment_form($args);
实例

在有表单的页面之间放入 <?php comments_template(); ?> 就可引入comments.php文件

在某些情况下,你希望以不同的方式来显示你的评论,这时可以建立一个自定义的文件(例如 other-comments.php),并且通过下面的方式调用:
  1. <?php comments_template( '/other-comments.php' ); ?>
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|5ucms.com

GMT+8, 2025-8-14 11:12 , Processed in 0.046875 second(s), 29 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表