Add Div to Comment Form

时间:2014-07-28 作者:Josh Rodgers

我有一些显示我的评论表单和自定义验证的代码,我想做的是在标题下面添加一个div,但我不知道如何做,因为该代码是生成的。。。

代码:

<?php $comment_args = array(
    \'title_reply\' => \'Leave a Comment\', 
    \'comment_notes_before\' => \'\',  
    \'fields\' => apply_filters( \'comment_form_default_fields\', array(
    \'author\' => \'<p class="comment-form-author">\' . \'<label for="author">\' . __( \'Name:\' ) . \'</label> \' . ( $req ? \'<span>*</span>\' : \'\' ) .
    \'<input id="author" name="author" type="text" value="\' . esc_attr( $commenter[\'comment_author\'] ) . \'" size="30" required /></p>\', 
    \'email\'  => \'<p class="comment-form-email">\' .
    \'<label for="email">\' . __( \'E-mail:\' ) . \'</label> \' . ( $req ? \'<span>*</span>\' : \'\' ) .
    \'<input id="email" name="email" type="text" value="\' . esc_attr(  $commenter[\'comment_author_email\'] ) . \'" size="30" required />\'.\'</p>\', 
    \'url\'    => \'\' ) ), 
    \'comment_field\' => \'<p>\' . \'<label for="comment">\' . __( \'Comment:\' ) . \'</label>\' . \'<textarea id="comment" name="comment" cols="45" rows="8" required></textarea>\' . \'</p>\', 
    \'comment_notes_after\' => \'\', 
    \'label_submit\' => __( \'Post\' ),
    );
    comment_form($comment_args);
?>
其输出:

<h3 id="reply-title" class="comment-reply-title">Leave a Comment <small><a rel="nofollow" id="cancel-comment-reply-link" href="/93/#respond" style="display:none;">Cancel reply</a></small></h3>

<form action="http://ourpictureshare.com/wp-comments-post.php" method="post" id="commentform" class="comment-form">
      <p class="logged-in-as">Logged in as <a href="http://ourpictureshare.com/wp-admin/profile.php">admin</a>. <a href="http://ourpictureshare.com/wp-login.php?action=logout&amp;redirect_to=http%3A%2F%2Fourpictureshare.com%2F93%2F&amp;_wpnonce=e330f94e35" title="Log out of this account">Log out?</a></p>
      <p><label for="comment">Comment:</label><textarea id="comment" name="comment" cols="45" rows="8" required=""></textarea></p>                                              
      <p class="form-submit">
          <input name="submit" type="submit" id="submit" value="Post">
          <input type="hidden" name="comment_post_ID" value="93" id="comment_post_ID">
          <input type="hidden" name="comment_parent" id="comment_parent" value="0">
      </p>
      <input type="hidden" id="_wp_unfiltered_html_comment_disabled" name="_wp_unfiltered_html_comment" value="70754f57d7"><script>(function(){if(window===window.parent){document.getElementById(\'_wp_unfiltered_html_comment_disabled\').name=\'_wp_unfiltered_html_comment\';}})();</script>
</form>
我在寻找什么:

<h3 id="reply-title" class="comment-reply-title">Leave a Comment <small><a rel="nofollow" id="cancel-comment-reply-link" href="/93/#respond" style="display:none;">Cancel reply</a></small></h3>

<!-- I added this line --> <div id="MY_DIV_HERE"></div> <!-- I added this line -->

<form action="http://ourpictureshare.com/wp-comments-post.php" method="post" id="commentform" class="comment-form">
    <p class="logged-in-as">Logged in as <a href="http://ourpictureshare.com/wp-admin/profile.php">admin</a>. <a href="http://ourpictureshare.com/wp-login.php?action=logout&amp;redirect_to=http%3A%2F%2Fourpictureshare.com%2F93%2F&amp;_wpnonce=e330f94e35" title="Log out of this account">Log out?</a></p>
    <p><label for="comment">Comment:</label><textarea id="comment" name="comment" cols="45" rows="8" required=""></textarea></p>                                              
    <p class="form-submit">
        <input name="submit" type="submit" id="submit" value="Post">
        <input type="hidden" name="comment_post_ID" value="93" id="comment_post_ID">
        <input type="hidden" name="comment_parent" id="comment_parent" value="0">
  </p>
  <input type="hidden" id="_wp_unfiltered_html_comment_disabled" name="_wp_unfiltered_html_comment" value="70754f57d7"><script>(function(){if(window===window.parent){document.getElementById(\'_wp_unfiltered_html_comment_disabled\').name=\'_wp_unfiltered_html_comment\';}})();</script>
</form>
非常感谢您的帮助。

谢谢,乔希

1 个回复
最合适的回答,由SO网友:birgire 整理而成

如果要在</h3> 以及<form> 标签,您可以尝试以下操作:

/**
 * Add custom HTML between the `</h3>` and the `<form>` tags in the comment_form() output.
 */
add_action( \'comment_form_before\', function(){
    add_filter( \'pre_option_comment_registration\', \'wpse_156112\' );
});

function wpse_156112( $comment_registration )
{
     // Adjust this to your needs:
     echo \'<div>Custom HTML between the closing H3 and opening FORM tags.</div>\'; 

    remove_filter( current_filter(), __FUNCTION__ );
    return $comment_registration;
}
其中输出为:

...</h3>
<div>Custom HTML between the closing H3 and opening FORM tags.</div>
<form>...
否则,您可以使用comment_form_top 打开后立即添加自定义HTML的操作<form> 标签:

/**
 * Add custom HTML just after the opening `<form>` tag in the comment_form() output.
 */
add_action( \'comment_form_top\', function(){
     // Adjust this to your needs:
     echo \'<div>Custom HTML just after the opening FORM tag.</div>\'; 
});
具有以下输出:

<form>
    <div>Custom HTML just after the opening FORM tag.</div>
    ...
在这里,您可以看到“TwentyEleven”主题的评论表单,这两种方法都已激活:

comment form

结束

相关推荐

以数组形式返回PAGINATE_COMMENTS_LINKS()

这个documentation 指定:类型(字符串)(可选)控制返回值的格式。可能的值包括:“普通”-链接由换行符分隔的字符串</“数组”-分页链接列表的数组,可完全控制显示为了将推特引导分页样式添加到wordpress评论分页中,我希望返回是一个数组。当我在通话中添加$args时,我得不到任何回报。我的代码:<ol class=\"commentlist\" style=\"list-style: none;\"> <?php wp_list_comments(\