comment_form()的$args参数详解

品味人生 • 发布于 2019-02-01 21:37:02

这个家伙很害羞,不想介绍自己!

comment_form()方法是wordpress 3.0版本之后出现的新方法,可以直接通过在页面中调用该方法来生成评论所需要用到的表单。大大降低了页面构建的难度,降低页面构建的出错性与代码的复用性。

但是随之也带来一个问题,就是无法再像wordpress 3.0版本之前那样通过修改源代码来直接更高评论表单的内容了。是不是真的就无法在模板中修改了呢?当然不是,comment_form()方法提供了两个参数,分别是$args和$post_id。使用方法为<?php comment_form($args, $post_id); ?>


  • $args:comment_form()的输出配置参数,为一个关联数组,配置项非常丰富

  • $post_id:文章id,默认为空,即当前id


下面以一个简单的实例来说明一下args配置数组的方法:
$defaults = array(
'fields' => apply_filters( 'comment_form_default_fields', $fields ),
'comment_field' => '<p class="comment-form-comment"><label for="commet">' . _x( 'Comment', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45″ rows="8″ aria-required="true"></textarea></p>',
'must_log_in' => '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
'logged_in_as' => '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : "" ) . '</p>',
'comment_notes_after' => '<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
'id_form' => 'commentform',
'id_submit' => 'submit',
'title_reply' => __( 'Leave a Reply' ),
'title_reply_to' => __( 'Leave a Reply to %s' ),
'cancel_reply_link' => __( 'Cancel reply' ),
'label_submit' => __( 'Post Comment' ),
);

通过上面的数组变量配置,基本上可以完成所有的自定义修改了,是不是很方便呢?