【问题标题】:Custom comment form in WordPressWordPress中的自定义评论表单
【发布时间】:2013-01-14 13:58:14
【问题描述】:

我想在 WordPress 中自定义 comment_form() 函数的输出。 要编辑字段和文本区域,我使用了以下代码,一切正常。

<?php
    $req = get_option( 'require_name_email' );
    $fields =  array(
            // redefine author field
        'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name:' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label>' .
        '<div class="input-prepend"><span class="add-on"><i class="icon-user"></i></span><input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . 'aria-required="true"' . ' required /></p>',
        'email'  => '<p class="comment-form-email"><label for="email">' . __( 'Email Address:' ) . ( $req ? ' <span class="required">*</span><br/>' : '' ) . '</label>' . 
        '<div class="input-prepend"><span class="add-on"><i class="icon-envelope"></i></span><input required id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . 'aria-required="true"' . ' required /></div></p>',
        'url'  => '<p class="comment-form-url"><label for="url">' . __( 'Your Website:' ) . '</label>' . 
        '<div class="input-prepend"><span class="add-on"><i class="icon-globe"></i></span><input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></div></p>'
        );
    $comments_args = array(
        'fields' => $fields,
            // redefine your own textarea (the comment body)
        'comment_field' => '<p class="comment-form-comment"><label for="comment">' . 'Comment' . '</label><textarea id="comment" name="comment" class="span12" rows="5" aria-required="true"></textarea></p>',
        );
    comment_form( $comments_args );
?>

这是结果:

现在,我想为提交按钮添加两个 CSS 类 (class="btn btn-primary");不使用 jQuery 怎么办?

注意:

关于解决方案,请阅读 cmets 关于标有绿色符号的答案。

【问题讨论】:

    标签: css wordpress comments


    【解决方案1】:

    正如 krizna 所说,您可以修改主题的 comments.php 文件以更改 comment_form() 的输出。我认为在这种情况下这可能是你最好的选择。

    如果您查看 Codex 中 comment_form()Function Reference,您会注意到您只能修改提交按钮的 ID,而不能修改类。 Trac 上有几张与此相关的票:

    如果在这种情况下修改 comments.php 对您不起作用,您可以尝试应用其中一个补丁。

    【讨论】:

    • 感谢您的回答。修补核心后,我无法将具有正确布局的提交按钮的主题分发给第三方。我可以使用 jQuery 添加我的类,但我想避免这种解决方案。还有其他方法吗?
    • 明白了。有什么办法可以修改相应的CSS?并使用提交 ID 代替 btnbtn-primary 类?
    • 没有。使用 ID,我将取消我的 CSS 的结构。
    • 我进行了更多挖掘,并在 WordPress Answers 上找到了类似的问题。其中一个回复建议创建一个新函数来替换 comment_form() 之一:wordpress.stackexchange.com/questions/19710/…
    • 老实说,迄今为止,这是解决此问题的最佳方法。感谢您在这件事上的帮助。
    猜你喜欢
    • 2013-07-27
    • 2012-02-01
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多