如何自定义Wordpress comment_form();

问题描述 投票:5回答:8

我目前正在开发自己的Wordpress主题,最近一直在研究自定义的comments_template();。我已经读过,使用wp_list_comments();方法是拉入并显示每页/帖子的评论的最佳实践。我已经成功地定制了通过该方法提取注释并显示的方式。

我还读到使用comment_form();方法是显示评论表单的最佳实践。但是,我真的在努力尝试定制这个。 $ args,过滤器和操作之间我有点困惑。

基本上我想彻底改变评论表格的部分内容。如何在仍然使用comment_form();方法的最佳实践的同时更改注释表单的部分内容?

我真正需要做的就是在<p>中包装几个现有的<divs>标签。我想要做的更新列表如下:

  1. <h3>标题调整为<h2 class="comments-header">Tell us about you!</h2>
  2. <fieldset></fieldset>中包装表单字段
  3. <label>包裹<div class="label"></div>
  4. <input>包裹<div class="field"></div>
  5. 在评论<p class="form-allowed-tags"></p>而不是之后显示<textarea>
  6. 更改表单提交按钮以使用<button>元素而不是<input>

请参阅以下代码以获得进一步说明......

默认comment_form();输出的代码:

<div id="respond">
    <h3 id="reply-title">Leave a Reply</h3>
    <form action="http://localhost/.../wp-comments-post.php" method="post" id="commentform">
        <p class="comment-notes">
            Your email address will not be published. Required fields are marked
            <span class="required">*</span>
        </p>
        <p class="comment-form-author">
            <label for="author">Name</label>
            <span class="required">*</span>
            <input id="author" name="author" type="text" value="John Doe" size="30" aria-required="true">
        </p>
        <p class="comment-form-email">
            <label for="email">Email</label>
            <span class="required">*</span>
            <input id="email" name="email" type="text" value="[email protected]" size="30" aria-required="true">
        </p>
        <p class="comment-form-url">
            <label for="url">Website</label>
            <input id="url" name="url" type="text" value size="30">
        </p>
        <p class="comment-form-comment">
            <label for="comment">Comment</label>
            <textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea>
        </p>
        <p class="form-allowed-tags">
            You may use these HTML tags and attributes...
        </p>
        <p class="form-submit">
            <input name="submit" type="submit" id="submit" value="Post Comment">
            <input type="hidden" name="comment_post_ID" value="22" id="comment_post_ID">
            <input type="hidden" name="comment_parent" id="comment_parent" value="0">
        </p>
    </form>
</div> <!-- #respond -->

代码我想输出:

<div id="respond">
    <h2 class="comments-header">Tell us about you!</h2>
    <form action="http://localhost/.../wp-comments-post.php" method="post" id="commentform">
        <fieldset>
            <div class="label"><label for="author">Name <span class="required">*</span></label></div>
            <div class="field"><input id="author" name="author" type="text" value="<?php echo $comment_author_email; ?>" size="30" aria-required="true"></div>
        </fieldset>
        <fieldset>
            <div class="label"><label for="email">E&ndash;mail (will not be published) <span class="required">*</span></label></div>
            <div class="field"><input id="email" name="email" type="text" value="<?php echo $comment_author_email; ?>" size="30" aria-required="true"></div>
        </fieldset>

        <p class="form-allowed-tags">
            You may use these HTML tags and attributes...
        </p>

        <fieldset>
            <div class="field"><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></div>
        </fieldset>

        <p class="form-submit">
            <button class="story-submit-btn" type="submit" name="submit" id="sub">Post your story</button>
            <input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" id="comment_post_ID">
            <input type="hidden" name="comment_parent" id="comment_parent" value="0">
        </p>
    </form>
</div> <!-- #respond -->

任何帮助是极大的赞赏!

php html wordpress comments wordpress-theming
8个回答
3
投票

我使用functions.php修改注释显示。我不知道现在是不是这样做了(我用WP开发的最后一个网站,需要评论是在2009年;)),但这里是(把它放在functions.php文件中!:

function THEMENAME_comment($comment, $args, $depth) {
  $GLOBALS['comment'] = $comment;
  *your comment display code*
}

记得要创建pingback主题。你这样做的评论类似,唯一不同的是第一行:

函数THEMENAME_pings($ comment,$ args,$ depth)

其他方式可能使用comments_template


3
投票

如果所有其他方法都失败了,请在主题目录的comments.php中将comment_form($args);更改为

ob_start();
comment_form($args);
$comment_form = ob_get_clean();
//insert code to modify $comment_form
echo $comment_form;

我用它将提交按钮更改为图像按钮。


2
投票

comment_form使用一组字段。该数组中的值是一个字符串。您可以通过为相应的键添加自己的字符串来覆盖字段的输出,如下所示:

    function modify_comment_fields($fields){

    $fields =  array('author' =>'<div><p class="comment-form-author"><label for="author">Author</label><input id="author" name="author" type="text"/></p></div>');

    return $fields;

    }

然后为您的函数添加一个过滤器:

add_filter('comment_form_default_fields','modify_comment_fields');

以下是comment_form函数中Codex的链接:http://codex.wordpress.org/Function_Reference/comment_form


2
投票
On Single.php after post content

Use it for custom comment form html and design

<code>  <?php $comments_args = array(
        // change the title of send button 
        'label_submit'=>'Submit',
        // change the title of the reply section
        'title_reply'=>'Add a comment',
        // remove "Text or HTML to be displayed after the set of comment fields"
        'comment_form_top' => 'ds',
        'comment_notes_before' => '',
        'comment_notes_after' => '',
        // redefine your own textarea (the comment body)
        'comment_field' => '<p class="comment-form-comment"><textarea id="comment" name="comment" placeholder="Your Comment* " aria-required="true"></textarea></p>',
        'fields' => apply_filters( 'comment_form_default_fields', array(

    'author' =>
      '<p class="comment-form-author">'  .
      '<input id="author" class="blog-form-input" placeholder="Your Name* " name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .
      '" size="30"' . $aria_req . ' /></p>',

    'email' =>
      '<p class="comment-form-email">'.
      '<input id="email" class="blog-form-input" placeholder="Your Email Address* " name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) .
      '" size="30"' . $aria_req . ' /></p>',

    'url' =>
      '<p class="comment-form-url">'.
      '<input id="url" class="blog-form-input" placeholder="Your Website URL" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .
      '" size="30" /></p>'
    )
  ),
);

comment_form($comments_args);?></code>

2
投票

简单示例如何更改某些注释表单字段。

$comments_args = array(
        // change the title of send button 
        'label_submit'=>'Send',
        // change the title of the reply section
        'title_reply'=>'Write a Reply or Comment',
        // 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">' . _x( 'Comment', 'noun' ) . '</label><br /><textarea id="comment" name="comment" aria-required="true"></textarea></p>',
);

comment_form($comments_args);

有关更多信息:comment_form() documentation on WordPress Codex


1
投票

你检查了手抄本:http://codex.wordpress.org/Function_Reference/comment_form

它不容易定制,但它是可行的


0
投票
function THEMENAME_comment($comment, $args, $depth) {
  $GLOBALS['comment'] = $comment;
  *your comment display code*
}

0
投票

我的主题使用了默认函数comment_form,因此我对子主题进行了更改。

    if(function_exists('glutton_comment_form'))
        glutton_comment_form(); //custom function for displaying comments, defined in glutton-child/functions.php
    else
        comment_form();
© www.soinside.com 2019 - 2024. All rights reserved.