WordPress的插件ACF给评论设置自定义标签后如何根据标签调取评论

  [复制链接]  19查看
易西 发表于 2025-9-1 14:30:13 | 显示全部楼层 |阅读模式
WordPress的插件ACF给评论设置自定义标签后如何根据标签调取评论
 楼主| 易西 发表于 2025-9-1 14:31:08 | 显示全部楼层


WordPress的插件ACF给评论设置自定义标签后如何根据标签调取评论
在WordPress中使用Advanced Custom Fields (ACF)插件来为评论添加自定义字段(即标签),然后根据这些标签来调取评论,可以通过几种方法实现。以下是一些常见的方法:

方法1:使用ACF的get_field函数
首先,确保你已经正确地在评论中使用了ACF的add_row或add_field_group来为评论添加了自定义字段。

1. 在评论表单中添加自定义字段
你可以在评论表单中使用ACF的acf_form()函数来添加自定义字段。例如:

acf_form(array(
    'post_id' => 'comment', // 设置评论的post_id为'comment'
    'field_groups' => array(123), // 替换为你的字段组ID
    'form' => '#commentform', // 替换为你的评论表单的ID或class
));
2. 在主题文件中获取评论的自定义字段
你可以在主题文件中使用get_field函数来获取评论的自定义字段值。例如,在循环显示评论时:

if ( have_comments() ) :
    while ( have_comments() ) : the_comment();
        $custom_field_value = get_field('your_custom_field_name', 'comment_' . get_comment_ID()); // 获取自定义字段值
        ?>
        <div class="comment">
            <p><?php comment_text(); ?></p>
            <p>Custom Field: <?php echo $custom_field_value; ?></p>
        </div>
    <?php
    endwhile;
endif;
方法2:使用ACF的get_posts函数(适用于更复杂的查询)
如果你需要基于自定义字段值来查询评论,可以使用ACF的get_posts函数。例如,获取特定标签的所有评论:

$args = array(
    'post_type' => 'comment', // 评论的类型,通常不需要更改,因为默认就是'comment'
    'posts_per_page' => -1, // 获取所有符合条件的评论
    'meta_key' => 'your_custom_field_name', // 你的自定义字段名
    'meta_value' => 'your_custom_field_value', // 你的自定义字段值
);
$comments = get_posts($args);
foreach ($comments as $comment) {
    echo '<div class="comment">';
    echo '<p>' . $comment->post_content . '</p>'; // 显示评论内容
    echo '</div>';
}
方法3:使用WP_Comment_Query类(更灵活的查询)
你也可以使用WordPress的WP_Comment_Query类来进行更灵活的查询。例如,基于自定义字段值查询评论:

$args = array(
    'meta_key' => 'your_custom_field_name', // 你的自定义字段名
    'meta_value' => 'your_custom_field_value', // 你的自定义字段值
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
foreach ($comments as $comment) {
    echo '<div class="comment">';
    echo '<p>' . get_comment_text($comment->comment_ID) . '</p>'; // 获取并显示评论内容
    echo '</div>';
}
以上方法可以帮助你根据ACF的自定义字段值来调取和显示评论。选择最适合你需求的方法来实现。

提示:AI自动生成,仅供参考

参考
[发帖际遇]: 一个袋子砸在了 易西 头上,易西 赚了 1 金钱. 幸运榜 / 衰神榜
 楼主| 易西 发表于 2025-9-1 14:45:41 | 显示全部楼层
ordPress的插件ACF给评论设置自定义标签
在使用WordPress的插件Advanced Custom Fields (ACF) 来为评论添加自定义字段时,你可以通过几种方法来实现。这里我将介绍几种常用的方法:

方法1:使用ACF的评论扩展
安装ACF Pro:首先确保你安装了ACF Pro,因为标准版的ACF不包含评论字段的功能。

启用评论扩展:在ACF的设置中启用“评论”扩展。这通常在ACF的设置页面中可以找到。

添加评论字段组:

在ACF后台,创建一个新的字段组。

选择“位置”选项卡,然后选择“评论”。

添加你需要的字段,例如文本、文本区域、复选框等。

在主题模板文件中使用这些字段:

if( function_exists('acf_form') ):
    acf_form(array(
        'post_id' => 'comment', // 这告诉ACF我们要在评论上工作
        'post_title' => false, // 隐藏标题字段
        'post_content' => false, // 隐藏内容字段
        'field_groups' => array('your_field_group_id') // 使用你的字段组ID
    ));
endif;

这段代码应该在你的评论表单模板中使用,比如在comments.php文件中。

方法2:使用ACF的钩子来保存和显示评论数据
如果你不想使用ACF的评论扩展,你可以通过以下方式手动处理:

添加自定义字段到评论:

add_action('comment_form', 'my_comment_form_fields');
function my_comment_form_fields() {
    echo '<p><label for="custom-field">Custom Field</label><input type="text" name="custom_field" id="custom-field" /></p>';
}

保存评论的自定义字段:

add_action('comment_post', 'save_custom_comment_meta');
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']));
    }
}

在评论模板中显示这些字段:

if (get_comment_meta($comment->comment_ID, 'custom_field', true)) {
    echo '<p>Custom Field: ' . get_comment_meta($comment->comment_ID, 'custom_field', true) . '</p>';
}

这段代码应该在你的comments.php模板中使用。

方法3:使用ACF的钩子显示自定义字段在评论中
如果你已经使用了ACF的标准方式添加了字段到某个帖子类型,你也可以在评论模板中显示这些字段,前提是这些字段与评论相关联(例如通过帖子ID):

if(function_exists('get_field')) {
    $value = get_field('your_field_name', 'comment_' . $comment->comment_ID);
    if($value) {
        echo '<p>' . $value . '</p>';
    }
}
这里'your_field_name'是你的ACF字段名称,而'comment_' . $comment->comment_ID是确保你访问的是与特定评论相关的字段值。

通过上述方法,你可以灵活地在WordPress评论中使用ACF自定义字段。选择最适合你需求的方法来实现。

提示:AI自动生成,仅供参考

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

本版积分规则

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

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

Powered by Discuz! X3.5

Copyright © , 吾侪网

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