Remove any existing snippets related to this topic.
Add this:
function make_star_bar() {
$rating = get_post_meta( get_the_ID(), 'rating', true );
if ( $rating ) {
$color = '#f00';
$prefix = 'star-bar-';
$uniqueClass = uniqid($prefix);
$percentage = 100 * $rating / 5;
$html = '<span class="'.$uniqueClass.'">★★★★★</span>
<style>
.'.$uniqueClass.' {
background: linear-gradient(90deg, '. $color . ' ' . $percentage .'%, rgba(0,0,0,0) '. $percentage.'%);
color: rgba(0,0,0,.2);
background-clip: text;
-webkit-background-clip: text;
color: rgba(0,0,0,.2);
}
</style>
';
return $html;
}
}
add_filter( 'render_block', function( $block_content, $block ) {
$stars = make_star_bar();
if ( $stars
&& ! empty( $block['attrs']['className'] )
&& 'star-bar' === $block['attrs']['className']
) {
$block_content = $stars;
}
return $block_content;
}, 10, 2 );
Then give the headline block a class of star-bar – i tested this in single posts and query loops.