WordPress如何在其他地方调用本篇文章作者的所有评论,并根据评论标签进行筛选调用

 火... [复制链接]  96查看
易西 发表于 2025-8-31 22:11:06 | 显示全部楼层 |阅读模式
WordPress如何在其他地方调用本篇文章作者的所有评论,并根据评论标签进行筛选调用

 楼主| 易西 发表于 2025-8-31 22:11:51 | 显示全部楼层

回答完成
搜索全网21篇资料
在WordPress中调用当前文章作者的所有评论并按标签筛选,可以通过以下步骤实现:

‌获取当前文章作者ID‌
使用get_the_author_meta('ID')获取当前文章作者ID
8。

‌查询该作者的所有评论‌
通过get_comments()函数查询该作者的评论,示例代码:

php
Copy Code
$author_id = get_the_author_meta('ID');
$args = array(
    'user_id' => $author_id,
    'status' => 'approve' //仅显示已审核评论
);
$comments = get_comments($args);
‌按标签筛选评论‌
需结合自定义评论元数据或分类法实现标签筛选
4。例如通过meta_query筛选带特定标签的评论:
php
Copy Code
$args = array(
    'user_id' => $author_id,
    'meta_key' => 'comment_tag',
    'meta_value' => 'featured', //标签值
    'meta_compare' => '='
);
‌完整实现示例‌
以下代码可放置在文章模板或自定义函数中:
php
Copy Code
$author_id = get_the_author_meta('ID');
$tag = 'featured'; //目标标签

$args = array(
    'user_id' => $author_id,
    'meta_query' => array(
        array(
            'key' => 'comment_tag',
            'value' => $tag
        )
    )
);

$filtered_comments = get_comments($args);
foreach ($filtered_comments as $comment) {
    echo '<div class="comment">';
    echo '<p>' . $comment->comment_content . '</p>';
    echo '</div>';
}
关键点说明:

需提前为评论添加标签元数据(如通过插件或自定义字段)
4
若需调用评论模板样式,可使用comments_template()函数
3
多标签筛选可通过WP_Query的tax_query实现类似文章分类的查询逻辑

参考
 楼主| 易西 发表于 2025-8-31 22:18:34 | 显示全部楼层
WordPress评论标签筛选调用方法
根据您提供的代码和问题,以下是关于WordPress如何根据评论标签进行筛选调用的详细解答:

基本评论查询方法
WordPress提供了get_comments()函数来查询和筛选评论,您可以通过多种参数来筛选评论:

php
Copy Code
$comments = get_comments(array(
    'post_id' => get_the_ID(), // 按文章ID筛选
    'type' => 'comment',       // 评论类型
    'status' => 'approve',     // 只获取已批准的评论
    'user_id' => get_the_author_meta('ID'), // 按用户ID筛选
    'number' => 10             // 获取数量
));
按标签筛选评论
虽然WordPress评论本身没有内置的"标签"系统,但您可以通过以下方式实现类似功能:

1. 使用评论元数据(comment_meta)
php
Copy Code
$tagged_comments = get_comments(array(
    'meta_key' => 'comment_tag',  // 元数据键名
    'meta_value' => '重要',       // 标签值
    'meta_compare' => '='         // 比较方式
));
2. 自定义分类法
您可以注册自定义分类法给评论使用:

php
Copy Code
// 注册评论标签分类法
function register_comment_tags() {
    register_taxonomy_for_object_type('comment_tag', 'comment');
}
add_action('init', 'register_comment_tags');

// 然后查询带有特定标签的评论
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'comment_tag',
            'field' => 'slug',
            'terms' => 'featured'
        )
    )
);
$tagged_comments = get_comments($args);
分页显示评论
如您提供的代码所示,可以实现评论分页:

php
Copy Code
$comments_per_page = 10;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$comments = get_comments(array(
    'number' => $comments_per_page,
    'offset' => ($paged - 1) * $comments_per_page
    // 其他筛选参数...
));

// 分页导航
$total_comments = get_comments(array('count' => true));
$total_pages = ceil($total_comments / $comments_per_page);

if ($total_pages > 1) {
    echo paginate_links(array(
        'base' => get_pagenum_link(1) . '%_%',
        'format' => 'page/%#%',
        'current' => $paged,
        'total' => $total_pages
    ));
}
前端交互实现
您可以使用jQuery实现评论的显示/隐藏功能:

javascript
Copy Code
jQuery(document).ready(function($) {
    $('.commentlist').hide();
    $('.comments-toggle').click(function() {
        $(this).next('.commentlist').slideToggle('fast');
    });
});
实际应用示例
根据您提供的代码,这里有一个实际应用示例,显示作者评论并实现"历史记录"切换功能:

php
Copy Code
// 显示1条可见评论
$visible_comment = get_comments(array(
    'post_id' => get_the_ID(),
    'type' => 'comment',
    'status' => 'approve',
    'user_id' => get_the_author_meta('ID'),
    'number' => 1
));

echo '<ul>';
foreach ($visible_comment as $comment) {
    echo '<div class="comment">';
    echo '<li style="test-align:none;">';
    echo get_comment_date('Y年m月d日');
    echo '<p>' . get_comment_text($comment->comment_ID) . '</p>';
    echo '</li>';
    echo '</div>';
}
echo '</ul>';
echo '<button class="comments-toggle">历史记录</button>';
echo '<ul class="commentlist">';
// 这里输出完整评论列表
echo '</ul>';
希望这些信息能帮助您实现WordPress评论的标签筛选和调用功能。如需进一步定制,可以根据具体需求调整查询参数和显示方式。

参考
[发帖际遇]: 易西 在网吧通宵,花了 7 金钱. 幸运榜 / 衰神榜
 楼主| 易西 发表于 2025-8-31 22:36:34 | 显示全部楼层
WordPress评论标签筛选功能实现方案
根据您的需求,您希望直接筛选出某个评论标签下的评论。我将基于您提供的代码文件,为您实现完整的评论标签筛选功能。

核心实现方案
1. 注册评论标签分类法
首先需要在WordPress中注册一个自定义分类法用于评论标签:

php
Copy Code
// 注册评论标签分类法
function register_comment_tags() {
    register_taxonomy(
        'comment_tag',
        'comment',
        array(
            'label' => '评论标签',
            'hierarchical' => false,
            'public' => true,
            'show_ui' => true
        )
    );
}
add_action('init', 'register_comment_tags');
2. 修改评论查询代码
在您的现有代码基础上,修改评论查询部分以支持标签筛选:

php
Copy Code
// 获取请求的标签参数
$requested_tag = isset($_GET['comment_tag']) ? sanitize_text_field($_GET['comment_tag']) : '';

// 构建基础查询参数
$base_args = array(
    'post_id' => get_the_ID(),
    'type' => 'comment',
    'status' => 'approve',
    'user_id' => get_the_author_meta('ID'),
    'orderby' => 'comment_date',
    'order' => 'DESC'
);

// 如果有标签筛选请求
if (!empty($requested_tag)) {
    $base_args['tax_query'] = array(
        array(
            'taxonomy' => 'comment_tag',
            'field' => 'slug',
            'terms' => $requested_tag
        )
    );
}

// 获取当前页评论
$current_comments_args = array_merge($base_args, array(
    'number' => $comments_per_page,
    'offset' => ($paged - 1) * $comments_per_page
));
$current_comments = get_comments($current_comments_args);
3. 添加标签筛选界面
在评论显示前添加标签筛选器:

php
Copy Code
// 获取所有可用标签
$tags = get_terms(array(
    'taxonomy' => 'comment_tag',
    'hide_empty' => false
));

// 显示标签筛选器
if (!empty($tags)) {
    echo '<div class="comment-tag-filter">';
    echo '<span>按标签筛选:</span>';
    echo '<a href="'.remove_query_arg('comment_tag').'" class="'.(empty($requested_tag) ? 'active' : '').'">全部</a>';
    foreach ($tags as $tag) {
        echo '<a href="'.add_query_arg('comment_tag', $tag->slug).'" class="'.($requested_tag == $tag->slug ? 'active' : '').'">'.$tag->name.'</a>';
    }
    echo '</div>';
}
4. 显示评论标签
在每条评论下方显示其标签:

php
Copy Code
// 显示评论标签
$comment_tags = get_the_terms($comment->comment_ID, 'comment_tag');
if (!empty($comment_tags)) {
    echo '<div class="comment-tags">';
    foreach ($comment_tags as $tag) {
        echo '<span class="comment-tag">'.$tag->name.'</span>';
    }
    echo '</div>';
}
完整实现代码
以下是整合了标签筛选功能的完整代码:

php
Copy Code
<?php
// 注册评论标签分类法
function register_comment_tags() {
    register_taxonomy(
        'comment_tag',
        'comment',
        array(
            'label' => '评论标签',
            'hierarchical' => false,
            'public' => true,
            'show_ui' => true
        )
    );
}
add_action('init', 'register_comment_tags');

// 获取请求的标签参数
$requested_tag = isset($_GET['comment_tag']) ? sanitize_text_field($_GET['comment_tag']) : '';

// 设置每页显示评论数
$comments_per_page = 10;
$paged = max(1, get_query_var('paged'));

// 构建基础查询参数
$base_args = array(
    'post_id' => get_the_ID(),
    'type' => 'comment',
    'status' => 'approve',
    'user_id' => get_the_author_meta('ID'),
    'orderby' => 'comment_date',
    'order' => 'DESC'
);

// 如果有标签筛选请求
if (!empty($requested_tag)) {
    $base_args['tax_query'] = array(
        array(
            'taxonomy' => 'comment_tag',
            'field' => 'slug',
            'terms' => $requested_tag
        )
    );
}

// 获取当前页评论
$current_comments_args = array_merge($base_args, array(
    'number' => $comments_per_page,
    'offset' => ($paged - 1) * $comments_per_page
));
$current_comments = get_comments($current_comments_args);

// 获取最新一条评论(用于折叠显示)
$latest_comment_args = array_merge($base_args, array('number' => 1));
$latest_comment = get_comments($latest_comment_args);

// 计算总评论数
$count_args = array_merge($base_args, array('count' => true));
$total_comments = get_comments($count_args);

// 获取所有可用标签
$tags = get_terms(array(
    'taxonomy' => 'comment_tag',
    'hide_empty' => false
));

// 显示标签筛选器
if (!empty($tags)) {
    echo '<div class="comment-tag-filter">';
    echo '<span>按标签筛选:</span>';
    echo '<a href="'.remove_query_arg('comment_tag').'" class="'.(empty($requested_tag) ? 'active' : '').'">全部</a>';
    foreach ($tags as $tag) {
        echo '<a href="'.add_query_arg('comment_tag', $tag->slug).'" class="'.($requested_tag == $tag->slug ? 'active' : '').'">'.$tag->name.'</a>';
    }
    echo '</div>';
}

// 显示最新一条评论
if (!empty($latest_comment)) {
    echo '<div class="latest-comment">';
    foreach ($latest_comment as $comment) {
        echo '<div class="comment-item">';
        echo '<div class="comment-date">' . get_comment_date('Y年m月d日') . '</div>';
        echo '<div class="comment-content">' . wpautop(get_comment_text($comment->comment_ID)) . '</div>';
        
        // 显示评论标签
        $comment_tags = get_the_terms($comment->comment_ID, 'comment_tag');
        if (!empty($comment_tags)) {
            echo '<div class="comment-tags">';
            foreach ($comment_tags as $tag) {
                echo '<span class="comment-tag">'.$tag->name.'</span>';
            }
            echo '</div>';
        }
        
        echo '</div>';
    }
    echo '</div>';
}

// 显示"显示更多"按钮(当评论多于1条时)
if ($total_comments > 1) {
    echo '<button class="comments-toggle">显示历史评论 (' . ($total_comments - 1) . ')</button>';
}

// 显示完整评论列表(初始隐藏)
echo '<div class="comment-list" style="display:none;">';

if (!empty($current_comments)) {
    foreach ($current_comments as $comment) {
        echo '<div class="comment-item">';
        echo '<div class="comment-date">' . get_comment_date('Y年m月d日') . '</div>';
        echo '<div class="comment-content">' . wpautop(get_comment_text($comment->comment_ID)) . '</div>';
        
        // 显示评论标签
        $comment_tags = get_the_terms($comment->comment_ID, 'comment_tag');
        if (!empty($comment_tags)) {
            echo '<div class="comment-tags">';
            foreach ($comment_tags as $tag) {
                echo '<span class="comment-tag">'.$tag->name.'</span>';
            }
            echo '</div>';
        }
        
        echo '</div>';
    }
} else {
    echo '<p class="no-comments">暂无更多评论</p>';
}

// 显示分页导航
if ($total_comments > $comments_per_page) {
    echo '<div class="comment-pagination">';
    echo paginate_links(array(
        'base' => add_query_arg('paged', '%#%'),
        'format' => '',
        'current' => $paged,
        'total' => ceil($total_comments / $comments_per_page),
        'prev_text' => __('« 上一页'),
        'next_text' => __('下一页 »'),
        'mid_size' => 2
    ));
    echo '</div>';
}

echo '</div>'; // 结束comment-list
?>

<script>
jQuery(document).ready(function($) {
    $('.comments-toggle').click(function() {
        $(this).next('.comment-list').slideToggle('fast');
        $(this).text(function(i, text) {
            return text === '显示历史评论 (<?php echo $total_comments - 1; ?>)'
                   ? '隐藏历史评论'
                   : '显示历史评论 (<?php echo $total_comments - 1; ?>)';
        });
    });
});
</script>
使用说明
‌添加标签‌

在WordPress后台"评论"部分,可以编辑评论并为其添加标签
‌前端筛选‌

页面顶部会显示所有可用标签作为筛选选项
点击标签即可筛选该标签下的评论
点击"全部"可取消筛选
‌保留原有功能‌

仍然保持最新评论显示和折叠功能
分页功能与标签筛选兼容
样式建议
css
Copy Code
.comment-tag-filter {
    margin-bottom: 20px;
}

.comment-tag-filter a {
    display: inline-block;
    margin-right: 10px;
    padding: 3px 8px;
    background: #f0f0f0;
    color: #333;
    text-decoration: none;
    border-radius: 3px;
}

.comment-tag-filter a.active {
    background: #1e73be;
    color: white;
}

.comment-tags {
    margin-top: 8px;
}

.comment-tag {
    display: inline-block;
    margin-right: 5px;
    padding: 2px 6px;
    background: #e6f2ff;
    color: #1e73be;
    font-size: 0.8em;
    border-radius: 3px;
}
这套实现方案完全满足了您直接筛选特定标签下评论的需求,同时保持了原有功能的完整性和良好的用户体验。

参考
[发帖际遇]: 易西 被钱袋砸中进医院,看病花了 5 金钱. 幸运榜 / 衰神榜
 楼主| 易西 发表于 2025-8-31 23:07:41 | 显示全部楼层
WordPress评论标签显示解决方案
根据您的问题echo <?php the_field('comment-tags'); ?>,结合提供的文件内容,以下是完整的解决方案:

问题分析
您想显示评论的标签,但the_field()函数通常用于高级自定义字段(ACF)插件,而评论标签是通过WordPress分类法实现的,需要使用不同的函数调用方式。

正确显示评论标签的方法
1. 基本显示方法
php
Copy Code
$comment_tags = get_the_terms($comment->comment_ID, 'comment_tag');
if (!empty($comment_tags)) {
    echo '<div class="comment-tags">';
    foreach ($comment_tags as $tag) {
        echo '<span class="comment-tag">'.$tag->name.'</span>';
    }
    echo '</div>';
}
2. 完整实现步骤
‌确保已注册评论标签分类法‌:
在主题的functions.php中添加:

php
Copy Code
function register_comment_tags() {
    register_taxonomy(
        'comment_tag',
        'comment',
        array(
            'label' => '评论标签',
            'hierarchical' => false,
            'public' => true,
            'show_ui' => true
        )
    );
}
add_action('init', 'register_comment_tags');
‌在评论模板中显示标签‌:
在显示每条评论的代码块中添加上述标签显示代码

‌添加CSS样式‌:

css
Copy Code
.comment-tags {
    margin-top: 8px;
}

.comment-tag {
    display: inline-block;
    margin-right: 5px;
    padding: 2px 6px;
    background: #e6f2ff;
    color: #1e73be;
    font-size: 0.8em;
    border-radius: 3px;
}
替代方案
如果您确实想使用类似the_field()的简洁方式,可以创建自定义函数:

php
Copy Code
function the_comment_tags($comment_id) {
    $tags = get_the_terms($comment_id, 'comment_tag');
    if (!empty($tags)) {
        echo '<div class="comment-tags">';
        foreach ($tags as $tag) {
            echo '<span class="comment-tag">'.$tag->name.'</span>';
        }
        echo '</div>';
    }
}
然后可以这样调用:

php
Copy Code
<?php the_comment_tags($comment->comment_ID); ?>
注意事项
确保评论已被正确分配标签
检查分类法名称是否为'comment_tag'
如果使用ACF插件为评论添加字段,需要使用get_field('field_name', 'comment_'.$comment_id)语法
以上解决方案基于提供的文件内容,实现了完整的评论标签显示功能,包括分类法注册、标签查询和前端显示。


参考
 楼主| 易西 发表于 2025-8-31 23:16:48 | 显示全部楼层
WordPress评论标签显示解决方案
根据您的问题echo <?php the_field('comment-tags'); ?>,结合提供的文件内容,以下是完整的解决方案:

问题分析
您想显示评论的标签,但the_field()函数通常用于高级自定义字段(ACF)插件,而评论标签是通过WordPress分类法实现的,需要使用不同的函数调用方式。

正确显示评论标签的方法
1. 基本显示方法
php
Copy Code
$comment_tags = get_the_terms($comment->comment_ID, 'comment_tag');
if (!empty($comment_tags)) {
    echo '<div class="comment-tags">';
    foreach ($comment_tags as $tag) {
        echo '<span class="comment-tag">'.$tag->name.'</span>';
    }
    echo '</div>';
}
2. 完整实现步骤
‌确保已注册评论标签分类法‌:
在主题的functions.php中添加:

php
Copy Code
function register_comment_tags() {
    register_taxonomy(
        'comment_tag',
        'comment',
        array(
            'label' => '评论标签',
            'hierarchical' => false,
            'public' => true,
            'show_ui' => true
        )
    );
}
add_action('init', 'register_comment_tags');
‌在评论模板中显示标签‌:
在显示每条评论的代码块中添加上述标签显示代码

‌添加CSS样式‌:

css
Copy Code
.comment-tags {
    margin-top: 8px;
}

.comment-tag {
    display: inline-block;
    margin-right: 5px;
    padding: 2px 6px;
    background: #e6f2ff;
    color: #1e73be;
    font-size: 0.8em;
    border-radius: 3px;
}
替代方案
如果您确实想使用类似the_field()的简洁方式,可以创建自定义函数:

php
Copy Code
function the_comment_tags($comment_id) {
    $tags = get_the_terms($comment_id, 'comment_tag');
    if (!empty($tags)) {
        echo '<div class="comment-tags">';
        foreach ($tags as $tag) {
            echo '<span class="comment-tag">'.$tag->name.'</span>';
        }
        echo '</div>';
    }
}
然后可以这样调用:

php
Copy Code
<?php the_comment_tags($comment->comment_ID); ?>
注意事项
确保评论已被正确分配标签
检查分类法名称是否为'comment_tag'
如果使用ACF插件为评论添加字段,需要使用get_field('field_name', 'comment_'.$comment_id)语法
以上解决方案基于提供的文件内容,实现了完整的评论标签显示功能,包括分类法注册、标签查询和前端显示。

参考
 楼主| 易西 发表于 2025-8-31 23:26:40 | 显示全部楼层
使用ACF插件调取分类评论内容的完整指南
根据您的问题"如何根据ACF插件的分类调取评论内容",结合提供的WordPress评论标签系统代码,以下是完整的解决方案:

一、基础配置
‌注册评论分类法‌(已在附件代码中实现)

代码中已通过register_comment_tags()函数注册了'comment_tag'分类法
分类法已关联到'comment'类型
‌ACF字段组创建‌

在ACF插件中创建字段组,选择"评论"作为位置规则
添加分类字段,设置分类法为'comment_tag'
二、调取评论内容的三种方法
方法1:使用标准WP_Comment_Query
php
Copy Code
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'comment_tag',
            'field'    => 'term_id',
            'terms'    => array(123), // 替换为您的分类ID
        ),
    ),
);
$comments_query = new WP_Comment_Query($args);
$comments = $comments_query->get_comments();
方法2:使用ACF字段值查询
php
Copy Code
$args = array(
    'meta_query' => array(
        array(
            'key'     => '您的ACF字段名', // 替换为您的ACF字段名
            'value'   => '分类值',
            'compare' => '=',
        ),
    ),
);
$comments = get_comments($args);
方法3:结合分类法和ACF字段(推荐)
php
Copy Code
// 获取特定ACF分类的term ID
$term = get_term_by('name', '您的分类名', 'comment_tag');

$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'comment_tag',
            'field'    => 'term_id',
            'terms'    => $term->term_id,
        ),
    ),
    'meta_query' => array(
        array(
            'key'     => '您的ACF字段名',
            'value'   => '特定值',
            'compare' => '=',
        ),
    ),
);
$comments = get_comments($args);
三、前端显示优化(基于附件代码)
‌增强标签筛选器‌(已在附件中实现)

代码中已包含按标签筛选评论的功能
通过$_GET['comment_tag']获取筛选参数
‌显示ACF自定义字段‌
在评论循环中添加:

php
Copy Code
foreach ($comments as $comment) {
    $custom_field = get_field('您的字段名', 'comment_'.$comment->comment_ID);
    if ($custom_field) {
        echo '<div class="custom-field">'.$custom_field.'</div>';
    }
}
四、完整实现示例
php
Copy Code
// 1. 获取ACF分类下的评论
$term = get_term_by('name', '重要', 'comment_tag');
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'comment_tag',
            'field'    => 'term_id',
            'terms'    => $term->term_id,
        ),
    ),
);

// 2. 执行查询
$comments = get_comments($args);

// 3. 显示评论
foreach ($comments as $comment) {
    echo '<div class="comment">';
    echo '<div class="author">'.$comment->comment_author.'</div>';
    echo '<div class="content">'.wpautop($comment->comment_content).'</div>';
   
    // 显示ACF字段
    $priority = get_field('priority', 'comment_'.$comment->comment_ID);
    if ($priority) {
        echo '<div class="priority">优先级: '.$priority.'</div>';
    }
   
    echo '</div>';
}
五、注意事项
ACF字段名称需与后台设置完全一致
评论ID需要使用'comment_'.$comment_ID格式
分类法名称必须为'comment_tag'(与注册时一致)
确保ACF字段组已正确关联到评论类型
以上解决方案结合了附件中的评论标签系统和ACF插件的功能,实现了按分类调取评论内容的需求。附件中的代码已经提供了良好的基础框架,只需添加ACF相关功能即可完整实现。

参考
 楼主| 易西 发表于 2025-8-31 23:57:40 | 显示全部楼层
WordPress中显示评论标签的方法
根据提供的PHP代码,以下是几种在WordPress中显示评论标签的有效方法:

1. 注册评论标签分类法
首先需要注册一个评论标签分类法,使评论支持标签功能:

php
Copy Code
function register_comment_tags() {
    register_taxonomy(
        'comment_tag',
        'comment',
        array(
            'label' => '评论标签',
            'hierarchical' => false,
            'public' => true,
            'show_ui' => true
        )
    );
}
add_action('init', 'register_comment_tags');
2. 获取并显示单个评论的标签
对于特定评论,可以使用以下方法获取并显示其标签:

php
Copy Code
// 方法一:使用get_the_terms()
$comment_tags = get_the_terms($comment_id, 'comment_tag');
if (!empty($comment_tags)) {
    echo '<div class="comment-tags">';
    foreach ($comment_tags as $tag) {
        echo '<span class="comment-tag">'.$tag->name.'</span>';
    }
    echo '</div>';
}

// 方法二:使用get_field()(如果使用ACF插件)
get_field('comment-tags', 'comment_'.$comment_id);
3. 显示评论列表时包含标签
在显示评论列表时,可以同时显示每条评论的标签:

php
Copy Code
$comments = get_comments(array(
    'post_id' => get_the_ID(),
    'status' => 'approve'
));

foreach ($comments as $comment) {
    echo '<div class="comment-item">';
    echo '<div class="comment-content">' . wpautop(get_comment_text($comment->comment_ID)) . '</div>';
   
    // 显示评论标签
    $comment_tags = get_the_terms($comment->comment_ID, 'comment_tag');
    if (!empty($comment_tags)) {
        echo '<div class="comment-tags">';
        foreach ($comment_tags as $tag) {
            echo '<span class="comment-tag">'.$tag->name.'</span>';
        }
        echo '</div>';
    }
   
    echo '</div>';
}
4. 添加标签筛选功能
可以创建一个标签筛选器,让用户按标签筛选评论:

php
Copy Code
// 获取所有可用标签
$tags = get_terms(array(
    'taxonomy' => 'comment_tag',
    'hide_empty' => false
));

// 显示标签筛选器
if (!empty($tags)) {
    echo '<div class="comment-tag-filter">';
    echo '<span>按标签筛选:</span>';
    echo '<a href="'.remove_query_arg('comment_tag').'" class="'.(empty($requested_tag) ? 'active' : '').'">全部</a>';
    foreach ($tags as $tag) {
        echo '<a href="'.add_query_arg('comment_tag', $tag->slug).'" class="'.($requested_tag == $tag->slug ? 'active' : '').'">'.$tag->name.'</a>';
    }
    echo '</div>';
}
5. 样式美化
可以为评论标签添加CSS样式:

css
Copy Code
.comment-tags {
    margin-top: 8px;
}

.comment-tag {
    display: inline-block;
    margin-right: 5px;
    padding: 2px 6px;
    background: #e6f2ff;
    color: #1e73be;
    font-size: 0.8em;
    border-radius: 3px;
}

.comment-tag-filter a {
    display: inline-block;
    margin-right: 10px;
    padding: 3px 8px;
    background: #f0f0f0;
    color: #333;
    text-decoration: none;
    border-radius: 3px;
}

.comment-tag-filter a.active {
    background: #1e73be;
    color: white;
}
通过以上方法,您可以在WordPress中有效地显示和管理评论标签,增强评论系统的功能和用户体验。

参考
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

Powered by Discuz! X3.5

Copyright © , 吾侪网

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