Archived topic
How to add ACF Star rating in loop
21 replies · Started by Martin on January 6, 2023
That's the query loop. Feel free to log in to the site. Thanks!
Hi Martin,
The login link you shared isn't working.
Can you check if it's correct?
They are working for me. I have attached another user login details. Thanks!
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.
Still nothing, unfortunately.
Are you able to login?
Thanks
Or I might found it now! Thanks
So what is this?
https://www.screencast.com/t/u3Gh1e4BQtP5
I checked that post and it has a rating of 3.5.
The other posts don't show any stars as they don't have a rating.