問題描述

我使用自定義過濾器來更改字段,但不能確定如何更改註釋表單中字段的順序。

期望訂單:

  • 評論欄位 (第一/上)

  • 名稱

  • 電子郵件

  • 網站

這是我目前使用的代碼:

function alter_comment_form_fields($fields){
    $fields['comments'] = 'Test';
    $fields['author'] = '<p class="comment-form-author">' . '<label for="author">' . __( 'Your name, please' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
                    '<input id="author" name="author" type="text" placeholder="John Smith" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>';
    $fields['email'] = 'next';  //removes email field
    //$fields['url'] = '';  //removes website field

    return $fields;
}

add_filter('comment_form_default_fields','alter_comment_form_fields');

最佳解決方案

這很簡單您只需將 textarea 從默認字段中刪除 – 過濾'comment_form_defaults' – 並將其打印在操作'comment_form_top'上:

<?php # -*- coding: utf-8 -*-
/**
 * Plugin Name: T5 Comment Textarea On Top
 * Description: Makes the textarea the first field of the comment form.
 * Version:     2012.04.30
 * Author:      Thomas Scholz <info@toscho.de>
 * Author URI:  http://toscho.de
 * License:     MIT
 * License URI: http://www.opensource.org/licenses/mit-license.php
 */

// We use just one function for both jobs.
add_filter( 'comment_form_defaults', 't5_move_textarea' );
add_action( 'comment_form_top', 't5_move_textarea' );

/**
 * Take the textarea code out of the default fields and print it on top.
 *
 * @param  array $input Default fields if called as filter
 * @return string|void
 */
function t5_move_textarea( $input = array () )
{
    static $textarea = '';

    if ( 'comment_form_defaults' === current_filter() )
    {
        // Copy the field to our internal variable …
        $textarea = $input['comment_field'];
        // … and remove it from the defaults array.
        $input['comment_field'] = '';
        return $input;
    }

    print apply_filters( 'comment_form_field_comment', $textarea );
}

次佳解決方案

我喜歡 toscho 回答。但是我想使用一個自定義 textarea,所以在這種情況下它不起作用。我使用相同的鈎子,但具有單獨的功能:

add_filter( 'comment_form_defaults', 'remove_textarea' );
add_action( 'comment_form_top', 'add_textarea' );

function remove_textarea($defaults)
{
    $defaults['comment_field'] = '';
    return $defaults;
}

function add_textarea()
{
    echo '<p class="comment-form-comment"><textarea id="comment" name="comment" cols="60" rows="6" placeholder="write your comment here..." aria-required="true"></textarea></p>';
}

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。