WordPress如何在后台评论的评论类型中添加评论类型并在帖子页面显示出来

  [复制链接]  44查看
易西 发表于 2025-9-1 13:48:15 | 显示全部楼层 |阅读模式
WordPress如何在后台评论的评论类型中添加评论类型并在帖子页面显示出来

 楼主| 易西 发表于 2025-9-1 13:50:56 | 显示全部楼层


WordPress如何在后台评论的评论类型中添加评论类型并在帖子页面显示出来
在WordPress中添加自定义评论类型并在帖子页面显示,可以通过以下步骤实现:

1. 注册自定义评论类型
首先,你需要注册一个或多个自定义评论类型。这可以通过在主题的 functions.php 文件中使用 register_comment_type() 函数来实现。例如,如果你想添加一个名为“问答”的评论类型,可以这样做:

function register_custom_comment_types() {
    register_comment_type('question', array(
        'labels' => array(
            'name' => _x('Questions', 'comment type general name'),
            'singular_name' => _x('Question', 'comment type singular name'),
        ),
    ));
}
add_action('init', 'register_custom_comment_types');
2. 修改评论表单以包含自定义评论类型
接下来,你需要修改评论表单,使其允许用户选择不同的评论类型。这可以通过修改 comment_form 的默认参数来实现:

function custom_comment_form_fields($fields) {
    $commenter = wp_get_current_commenter();
    $fields['comment_type'] = '<p class="comment-form-type"><label for="comment_type">' . __('Comment as') . '</label><select name="comment_type" id="comment_type">';
    $fields['comment_type'] .= '<option value="comment">' . __('Comment') . '</option>';
    $fields['comment_type'] .= '<option value="question">' . __('Question') . '</option>'; // 添加自定义评论类型选项
    $fields['comment_type'] .= '</select></p>';
    return $fields;
}
add_filter('comment_form_default_fields', 'custom_comment_form_fields');
3. 显示自定义评论类型
在帖子页面显示评论时,你需要确保自定义评论类型被正确显示。你可以通过修改 comments_template 函数来调整显示方式:

function custom_comments_template($template) {
    if (is_singular() && comments_open() && get_option('thread_comments')) {
        $custom_template = locate_template(array('custom-comments.php')); // 自定义模板文件路径
        if (!empty($custom_template)) {
            return $custom_template;
        } else {
            return plugin_dir_path(__FILE__) . 'custom-comments.php'; // 或者使用插件目录下的模板文件路径
        }
    }
    return $template;
}
add_filter('comments_template', 'custom_comments_template');
4. 创建自定义评论模板(custom-comments.php)
在你的主题目录或插件目录中创建一个名为 custom-comments.php 的文件,并在其中编写代码来区分不同评论类型的显示:

<?php if (have_comments()) : ?>
    <h3><?php comments_number(__('No Comments'), __('1 Comment'), __('% Comments')); ?></h3>
    <ol class="commentlist">
        <?php wp_list_comments(array('callback' => 'custom_comments')); ?>
    </ol>
<?php endif; ?>
<?php comment_form(); ?>
然后,创建一个回调函数来区分不同评论类型的显示:

function custom_comments($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment; ?>
    <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
        <article id="comment-<?php comment_ID(); ?>" class="comment">
            <footer>
                <?php echo get_avatar($comment, 40); ?>
                <div class="comment-author"><?php printf(__('<cite class="fn">%s</cite>'), get_comment_author_link()) ?></div> <!-- Comment author -->
                <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars(get_comment_link($comment->comment_ID)) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','') ?></div> <!-- Comment date and Edit link -->
            </footer>
            <?php if ($comment->comment_approved == '0') : ?> <!-- If comment is pending -->
                <em><?php _e('Your comment is awaiting moderation.') ?></em
提示:AI自动生成,仅供参考


参考
 楼主| 易西 发表于 2025-9-1 13:58:20 | 显示全部楼层
在WordPress中添加自定义评论类型并显示在帖子页面,可以通过以下步骤实现:

‌注册自定义评论类型‌

在主题的functions.php文件中添加代码注册新的评论类型,使用register_meta函数为评论添加元数据字段
6。例如:
php
Copy Code
add_action('init', 'register_custom_comment_type');
function register_custom_comment_type() {
    register_meta('comment', 'custom_field_name', array(
        'type' => 'string',
        'description' => 'Custom comment field',
        'single' => true,
        'show_in_rest' => true
    ));
}
‌在评论表单中添加自定义字段‌

使用comment_form_default_fields过滤器添加表单字段
7
10。例如:
php
Copy Code
add_filter('comment_form_default_fields', 'add_custom_comment_field');
function add_custom_comment_field($fields) {
    $fields['custom_field'] = '<p><label for="custom_field">Custom Field</label><input type="text" name="custom_field" id="custom_field"></p>';
    return $fields;
}
‌保存自定义字段数据‌

使用comment_post钩子在提交评论时保存字段数据
4:
php
Copy Code
add_action('comment_post', 'save_custom_comment_field');
function save_custom_comment_field($comment_id) {
    if (isset($_POST['custom_field'])) {
        update_comment_meta($comment_id, 'custom_field_name', sanitize_text_field($_POST['custom_field']));
    }
}
‌在帖子页面显示自定义评论类型‌

修改主题的comments.php文件,使用get_comment_meta函数输出自定义字段
5
6。例如:
php
Copy Code
$custom_field = get_comment_meta($comment->comment_ID, 'custom_field_name', true);
if ($custom_field) {
    echo '<div class="custom-field">Custom Field: ' . esc_html($custom_field) . '</div>';
}
‌在后台管理界面显示自定义字段‌

使用add_meta_boxes钩子在评论编辑界面添加元框
4:
php
Copy Code
add_action('add_meta_boxes_comment', 'add_custom_comment_meta_box');
function add_custom_comment_meta_box() {
    add_meta_box('custom-comment-field', 'Custom Field', 'render_custom_comment_meta_box', 'comment', 'normal', 'high');
}
function render_custom_comment_meta_box($comment) {
    $custom_field = get_comment_meta($comment->comment_ID, 'custom_field_name', true);
    echo '<input type="text" name="custom_field" value="' . esc_attr($custom_field) . '" style="width:100%">';
}
‌保存后台编辑的字段数据‌

使用edit_comment钩子保存后台编辑的数据
4:
php
Copy Code
add_action('edit_comment', 'save_admin_custom_comment_field');
function save_admin_custom_comment_field($comment_id) {
    if (isset($_POST['custom_field'])) {
        update_comment_meta($comment_id, 'custom_field_name', sanitize_text_field($_POST['custom_field']));
    }
}
通过以上步骤,可以在WordPress中实现自定义评论类型的添加和显示功能
4
5。

参考
[发帖际遇]: 一个袋子砸在了 易西 头上,易西 赚了 1 金钱. 幸运榜 / 衰神榜
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

邮箱|首页|小黑屋|吾侪 ( 蜀ICP备2020029307号-4 )

GMT+8, 2026-8-3 07:15 , Processed in 0.066263 second(s), 23 queries .

Powered by Discuz! X3.5

Copyright © , 吾侪网

快速回复 返回顶部 返回列表