Comment Meta

Comment Meta containers向评论编辑屏幕添加额外的字段。 每个评论的字段数据作为评论元单独存储(参见 add_comment_meta)。

use Carbon_Fields\Container;
use Carbon_Fields\Field;

Container::make( 'comment_meta', __( 'Comment Information' ) )
    ->add_fields( array(
        Field::make( 'text', 'crb_comment_rating', __( 'Comment Rating' ) ),
        Field::make( 'text', 'crb_comment_additional_info', __( 'Additional Comment Information' ) ),
    ) );

访问字段值

要访问字段值,您需要使用函数 carbon_get_comment_meta( $comment_id, $name ),其中:

参数 描述
$comment_id 在其中输入值的注释 ID。
$name 要检索的字段的字段名称模式。
<?php
$comments = get_comments( array(
    'post_id' => get_the_ID(),
) );

foreach ( $comments as $comment ) {
    $comment_additional_info = carbon_get_comment_meta( $comment->comment_ID, 'crb_comment_additional_info' );
    $comment_rating    = carbon_get_comment_meta( $comment->comment_ID, 'crb_comment_rating' );

    if ( ! empty( $comment_additional_info ) ) {
        echo $comment->comment_ID . ' info: ' . $comment_additional_info;
    }

    if ( ! empty( $comment_rating ) ) {
        echo 'Rating: ' . $comment_rating;
    }
}
?>