I think you are looking for this-
function star_rating( $atts ) {
$max = 5;
$atts = shortcode_atts(
array(
'grade' => $max, // default
), $atts );
$star_icons="";
$full_star = floor( $atts['grade'] );
for( $i=1; $i<=$full_star; $i++ ) {
$star_icons .= '<i class="fa fa-star"></i>';
}
$half_star = 0;
if( (int) $atts['grade'] != $atts['grade'] ){
$half_star = 1;
$star_icons .= '<i class="fa fa-star-half-o"></i>';
}
//considering you want to show empty stars as well
$empty_star = $max - $half_star - $full_star;
for( $i=1; $i<=$empty_star; $i++ ) {
$star_icons .= '<i class="fa fa-star-o"></i>';
}
return $star_icons;
}
add_shortcode( 'star_rating', 'star_rating' );
Note:
- all fraction numbers are considered as 1 half-star. I mean, 2.5 and 2.6 will produce same output.
- these type of stuffs should be handled with CSS.
- Tested and working.