Archived topic
Star Rating System
43 replies · Started by russel on December 2, 2021
1. You would remove that snippet.
And use this instead:
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>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Rating",
"ratingValue": "' . $rating . '",
"bestRating": "5",
"worstRating": "1"
}
</script>
';
return $html;
}
}
add_shortcode('star_bar', 'make_star_bar');
2. Then create a Block Element Hook in Appearance -> Elements:
https://docs.generatepress.com/article/block-element-hook/
2.1 Add your Content and the [star_bar] shortcode to the element.
2.2 Set the Hook to after_entry_title
2.3 Set the Display Rule Location to where its required eg. Post > All Posts
3. Publish that Element.
4. Now you need to create a Custom Field on your post called: rating - which is where you will add the rating value for each post.
Unfortunately, it not working despite completing all 4 steps... Only showing [star_bar]. I also noticed the star rating appears if I add the shortcode [star_bar stars="2.6"] directly to the post, although it takes the value from the custom field now ( I add the shortcode [star_bar stars="2.6"], but it shows 4 stars entered in custom field).
Did you remove that other function ? And clear any caches ?
Yes, here are the screenshots:
https://ibb.co/Gc9m5Q7
https://ibb.co/ZVLYVt7
https://ibb.co/y0WQpJN
https://ibb.co/QJLjtmz
https://ibb.co/Nrn6wXc
What i mean is, do you still have the other snippets saved anywhere ?
nope, just tried to test if the new snippet works with the old shortcode [star_bar stars=”2.6″] and it did. Anyway it's not working with Custom Field: rating
I tested the steps i added here:
https://generatepress.com/forums/topic/star-rating-system/page/3/#post-2548930
And this my ACF Field:
https://www.screencast.com/t/kS9Z4DwuodN
And that works as intended using the [star_bar] shortcode.
So i am not sure why it won't work for you.
Still not working;/// Where do you put the actual rating value, and in what format? E.g I'm trying to add 4.0 or 4 in the custom field but it doesn't change anything...
My ACF Field Group has is set to Display on ALL posts:
https://www.screencast.com/t/XSExDdEd
then in the single Post editor, i added the value in the ACF field.
My settings are the same, although I also tried to test for 1 post only. And nothing has changed, I have the same issue, the [star_bar] appears instead of the visual star rating...
If you want to raise a new topic, and use the Private Information field to provide me an admin login i can take a closer look.
Hey David, are you still available for help?
If you can raise a new topic, where you can share a link to the site then i can take a look.
I cannot say i can make it work, as it iss custom development. But i am happy to look.
The new topic with private info created.