首先在评论表单上添加1-5*(星形)单选按钮。
add_action( \'comment_form_logged_in_after\', \'add_review_field_to_comment_form\' );
add_action( \'comment_form_after_fields\', \'add_review_field_to_comment_form\' );
function add_review_field_to_comment_form () {
echo \'<p class="comment-form-rating">\'.
\'<label for="rating">Rating</label>
<span class="commentratingbox">\';
for( $i=1; $i <= 5; $i++ )
echo \'<span class="commentrating"><input type="radio" name="rating" id="rating" value="\'. $i .\'"/>\'. $i .\'</span>\';
echo\'</span></p>\';
}
现在将评级值保存到comment meta。
add_action( \'comment_post\', \'save_comment_rating_data\' );
function save_comment_rating_data( $comment_id ) {
if ( ( isset( $_POST[\'rating\'] ) ) && ( $_POST[\'rating\'] != \'\') )
$rating = wp_filter_nohtml_kses($_POST[\'rating\']);
add_comment_meta( $comment_id, \'rating\', $rating );
}
现在显示评分(如果有)。
add_filter( \'comment_text\', \'display_rating_on_comment\');
function display_rating_on_comment( $text ){
if( $commentrating = get_comment_meta( get_comment_ID(), \'rating\', true ) ) {
$commentrating = \'Rating : \'.$commentrating.\' STAR\';
$text = $text . $commentrating;
return $text;
} else {
return $text;
}
}