如何将这个评论类型显示在评论框中,并根据评论类型调取评论呢

[复制链接]  15查看
易西 发表于 2025-9-1 11:36:54 | 显示全部楼层 |阅读模式
如何将这个评论类型显示在评论框中,并根据评论类型调取评论呢

[发帖际遇]: 易西 被钱袋砸中进医院,看病花了 4 金钱. 幸运榜 / 衰神榜
 楼主| 易西 发表于 2025-9-1 11:37:33 | 显示全部楼层
WordPress自定义评论类型显示与调取完整方案
根据您的需求,以下是实现自定义评论类型在评论框中显示并根据类型调取评论的完整解决方案:

一、评论表单添加类型选择字段
要在评论框中添加类型选择功能,可以通过以下两种方式实现:

1. 通过修改评论模板添加单选按钮组
php
Copy Code
// 在functions.php中添加
function add_comment_type_select($fields) {
    $types = array(
        'standard' => '标准评论',
        'custom' => '自定义评论'
    );
   
    $fields['comment_type'] = '<p class="comment-form-comment-type">
        <label for="comment_type">评论类型:</label>
        <select name="comment_type" id="comment_type">
            <option value="">请选择类型</option>';
            
    foreach($types as $key => $label) {
        $fields['comment_type'] .= '<option value="'.$key.'">'.$label.'</option>';
    }
   
    $fields['comment_type'] .= '</select></p>';
    return $fields;
}
add_filter('comment_form_default_fields', 'add_comment_type_select');
2. 使用插件添加类型选择(推荐)
安装"Comment Form Fields"插件,可以更灵活地添加评论类型选择字段,支持单选、多选等多种形式
20。

二、修改评论模板显示自定义类型
1. 创建自定义评论模板
在主题目录下创建custom-comments.php文件:

php
Copy Code
<?php
/**
* 自定义评论类型显示模板
* 使用方式:comments_template('custom-comments.php', true);
*/
if (!comments_open() && get_comments_number() == '0') {
    return;
}
?>
<div class="custom-comments">
    <?php
    $args = array(
        'type' => 'custom',
        'status' => 'approve',
        'order' => 'ASC'
    );
   
    $comments = get_comments($args);
   
    if ($comments) {
        wp_list_comments(array(
            'style' => 'div',
            'short_ping' => true,
            'avatar_size' => 48,
            'callback' => 'custom_comment_callback'
        ));
    } else {
        echo '<p>暂无自定义评论</p>';
    }
    ?>
</div>
2. 添加自定义评论显示回调函数
php
Copy Code
function custom_comment_callback($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment;
    ?>
    <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
        <div id="comment-<?php comment_ID(); ?>" class="comment-body">
            <div class="comment-author vcard">
                <?php echo get_avatar($comment, $args['avatar_size']); ?>
                <cite class="fn"><?php comment_author_link() ?></cite>
                <span class="comment-type">[自定义评论]</span>
            </div>
            <div class="comment-content">
                <?php comment_text() ?>
                <div class="comment-meta">
                    <a href="<?php echo esc_url(get_comment_link($comment->comment_ID)); ?>">
                        <?php
                        printf(
                            __('%1$s at %2$s'),
                            get_comment_date(),
                            get_comment_time()
                        );
                        ?>
                    </a>
                    <?php edit_comment_link(__('(编辑)'), '  ', '') ?>
                </div>
            </div>
        </div>
    <?php
}
三、前端按类型筛选评论
1. 添加评论类型筛选控件
php
Copy Code
// 在functions.php中添加
function add_comment_type_filter() {
    $types = array(
        'all' => '所有评论',
        'standard' => '标准评论',
        'custom' => '自定义评论'
    );
   
    echo '<div class="comment-filter">
        <label for="comment-filter">筛选评论类型:</label>
        <select id="comment-filter" class="comment-filter-select">
            ';
            
    foreach($types as $key => $label) {
        echo '<option value="'.$key.'">'.$label.'</option>';
    }
   
    echo '</select>
    </div>';
}
add_action('comment_form_before', 'add_comment_type_filter');
2. 添加JavaScript实现动态筛选
javascript
Copy Code
document.addEventListener('DOMContentLoaded', function() {
    const filterSelect = document.getElementById('comment-filter');
    const commentsContainer = document.querySelector('.custom-comments');
   
    filterSelect.addEventListener('change', function() {
        const type = this.value;
        const comments = document.querySelectorAll('.comment-body');
        
        comments.forEach(comment => {
            if (type === 'all' ||
                (type === 'standard' && !comment.classList.contains('custom-comment')) ||
                (type === 'custom' && comment.classList.contains('custom-comment'))) {
                comment.style.display = 'block';
            } else {
                comment.style.display = 'none';
            }
        });
    });
});
四、完整实现步骤总结
‌设置自定义评论类型‌:使用preprocess_comment过滤器标记评论类型
1
‌修改评论表单‌:添加类型选择字段(单选/下拉菜单)
16
20
‌创建自定义模板‌:为不同类型评论创建专用显示模板
25
27
‌实现前端筛选‌:通过JavaScript动态显示/隐藏特定类型评论
28
‌样式优化‌:为不同类型评论添加视觉区分
22
五、注意事项
确保在修改评论模板前备份原文件
如果使用插件方法,注意插件兼容性
21
自定义评论类型不会自动显示在前端,需要专门开发显示逻辑
14
对于大量评论,建议添加分页功能提升性能
9
通过以上方案,您可以完整实现自定义评论类型的显示、提交和筛选功能,满足多样化的评论管理需求。

参考
[发帖际遇]: 易西 捡了钱没交公 威望 降了 2 . 幸运榜 / 衰神榜
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-4 15:31 , Processed in 0.049213 second(s), 22 queries , APCu On.

Powered by Discuz! X3.5

Copyright © , 吾侪网

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