WordPress如何给评论添加标记根据标记调用评论内容

  [复制链接]  55查看
易西 发表于 2025-9-1 11:11:09 | 显示全部楼层 |阅读模式
WordPress如何给评论添加标记根据标记调用评论内容

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


WordPress如何给评论添加标记根据标记调用评论内容
在WordPress中,给评论添加自定义字段或标记,并根据这些标记调用评论内容,可以通过几种不同的方法实现。下面是一些常用的方法:

方法1:使用自定义字段(Custom Fields)
添加自定义字段到评论

你可以通过插件(如“Add Custom Fields to Comments”)或手动方式在评论表单中添加隐藏字段或通过JavaScript动态添加字段。例如,使用JavaScript可以这样做:

jQuery(document).ready(function($) {
    $('form#commentform').append('<input type="hidden" name="custom_field" value="your_value">');
});

在评论表单中添加字段

如果你选择手动方式,可以在comments.php模板文件中添加一个隐藏的输入字段:

<div class="comment-form-custom">
    <input type="hidden" name="custom_field" value="your_value">
</div>

在主题函数中保存自定义字段

在functions.php文件中,你可以使用comment_post钩子来保存自定义字段的值:

function save_custom_comment_meta($comment_id) {
    if ( isset($_POST['custom_field']) ) {
        update_comment_meta($comment_id, 'custom_field', sanitize_text_field($_POST['custom_field']));
    }
}
add_action('comment_post', 'save_custom_comment_meta');

调用自定义字段的值

在需要显示评论的地方,可以使用get_comment_meta函数来获取并显示自定义字段的值:

$custom_value = get_comment_meta($comment->comment_ID, 'custom_field', true);
echo $custom_value; // 显示自定义字段的值

方法2:使用评论类型(Comment Type)
定义评论类型

你可以通过插件(如“Comment Types”)或手动方式定义评论类型。例如,使用代码定义一个新评论类型:

function custom_comment_type($commentdata) {
    $commentdata['comment_type'] = 'custom'; // 自定义类型名称
    return $commentdata;
}
add_filter('preprocess_comment', 'custom_comment_type');

在评论查询中使用评论类型

在查询评论时,可以指定评论类型:

$args = array(
    'type' => 'custom', // 使用定义的评论类型名称
);
$comments = get_comments($args);

方法3:使用评论的类别(Categories)或标签(Tags)
虽然WordPress默认不直接支持将类别或标签应用于评论,但你可以通过插件(如“Comments Categories”)或创建自定义表来实现。然而,这种方法较为复杂且不是WordPress原生功能。通常,自定义字段或标记是实现此类需求的更简单和直接的方法。

结论
对于大多数情况,使用自定义字段是添加和调用评论标记的最简单和最直接的方法。这种方法不需要额外的插件,只需简单的PHP代码即可实现。如果你需要更复杂的功能,如基于评论类型的过滤,可能需要考虑使用插件或扩展WordPress核心功能。



参考
[发帖际遇]: 易西 发帖时在路边捡到 3 金钱,偷偷放进了口袋. 幸运榜 / 衰神榜
 楼主| 易西 发表于 2025-9-1 11:23:30 | 显示全部楼层
要在WordPress评论框中显示自定义评论类型,可以通过以下步骤实现:

添加评论类型选择控件
在评论表单中添加下拉选择框,让用户可以选择评论类型
11:
html
Copy Code
<p class="comment-form-type">
    <label for="comment_type">评论类型</label>
    <select name="comment_type" id="comment_type">
        <option value="comment">普通评论</option>
        <option value="custom">自定义评论</option>
        <option value="feedback">意见反馈</option>
    </select>
</p>
修改评论处理函数
更新之前的自定义评论类型处理函数,从表单获取选择的类型
11:
php
Copy Code
function custom_comment_type($commentdata) {
    if(isset($_POST['comment_type'])) {
        $commentdata['comment_type'] = sanitize_text_field($_POST['comment_type']);
    }
    return $commentdata;
}
add_filter('preprocess_comment', 'custom_comment_type');
显示当前评论类型
在评论列表模板中显示每条评论的类型
9:
php
Copy Code
function display_comment_type($comment_ID = 0) {
    $type = get_comment_type($comment_ID);
    echo '<span class="comment-type">'.esc_html($type).'</span>';
}
样式优化
添加CSS样式美化评论类型显示
9:
css
Copy Code
.comment-form-type {
    margin-bottom: 15px;
}
.comment-type {
    display: inline-block;
    padding: 2px 8px;
    background: #f0f0f0;
    border-radius: 12px;
    font-size: 0.8em;
}
完整实现流程
将表单HTML代码添加到主题的comments.php模板中
确保functions.php包含处理函数
在评论显示循环中调用display_comment_type()
通过CSS调整显示效果
这种方法保持了WordPress原生评论系统的完整性,同时增加了类型分类功能

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

本版积分规则

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

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

Powered by Discuz! X3.5

Copyright © , 吾侪网

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