- This topic has 43 replies, 4 voices, and was last updated 3 years ago by
Bernardas.
-
AuthorPosts
-
December 2, 2021 at 10:28 am #2036154
David
StaffCustomer SupportIf you’re cool with Apple Emojis and you don’t mind there being no decimal points for the stars then remove your current shortcode ( save it somewhere for backup ) and use this instead:
function make_star_bar( $atts ) { $value = shortcode_atts( array( 'stars' =>10 ),$atts); $html = '<ul class="star-bar">'; for ($x = 0; $x <= 10; $x++ ) { if ( $x <= $value['stars'] ) { $html .= '<li class="rated">🍎</li>'; } else { $html .= '<li>🍎</li>'; } } $html .= '</ul>'; return $html; } add_shortcode('star_bar', 'make_star_bar');Replace the apples with your previous unicodes.
And then add this CSS to your site:
.star-bar { list-style: none; margin: 0; display: flex; } .star-bar li { text-align: center; font-size: 50px; } .star-bar li:not(.rated) { opacity: 0.5; -webkit-filter: grayscale(100%); filter: grayscale(100%); }How to add CSS: https://docs.generatepress.com/article/adding-css/
December 2, 2021 at 8:17 pm #2036635russel
Works Great!
but like you said there are no decimal value but i can live with that
thank you
December 3, 2021 at 3:38 am #2036939David
StaffCustomer SupportAwesome – glad to hear that!!
November 26, 2022 at 1:54 am #2435250Jasmin
Hello David,
I love the Star Bar Shortcode!
But as pointed out by Nichola in GitHub, it doesn’t work if you use it multiple times on one page.
How easy is it to tweak the code so it can be used multiple times?
Thank you.
November 26, 2022 at 4:35 am #2435454David
StaffCustomer SupportHi there,
it depends 🙂 How are you using the shortcode – Is it inside a single post ? Or are you using it in the archives ?
November 26, 2022 at 4:36 am #2435459Jasmin
Using it in a single post 🙂
November 26, 2022 at 5:14 am #2435511David
StaffCustomer SupportSee my updated GIST here:
https://gist.github.com/diggeddy/ef3dc0315a73c0caad839c5348b89aa6
November 26, 2022 at 7:22 am #2435708Jasmin
Awesome! It works perfectly. Thanks, David.
November 27, 2022 at 3:52 am #2436874David
StaffCustomer SupportGlad to hear that!
February 25, 2023 at 2:27 pm #2546941Bernardas
For me, it’s always showing 5 empty stars no matter what number I add to the shortcode…
February 26, 2023 at 6:47 am #2547378David
StaffCustomer SupportHi there,
if you can share a link to your site where the issue can be seen, we can take a look,
February 26, 2023 at 2:03 pm #2547820Bernardas
Thanks, David, unfortunately, I can’t share the link. Actually, your code it’s working. The problem is with code with implemented Schema Rating ( I found it on your github.com link) I’m receiving an error: There has been a critical error on this website” when trying to add this:
function make_star_bar($atts)
{
$value = shortcode_atts(array(
‘stars’ => 5,
‘color’ => ‘#f00’
), $atts);
$prefix = ‘star-bar-‘;
$uniqueClass = uniqid($prefix);
$percentage = 100 * $value[‘stars’] / 5;
$html = ‘<span class=”‘ . $uniqueClass . ‘”>★★★★★</span>
<style>
.’ . $uniqueClass . ‘ {
background: linear-gradient(90deg, ‘ . $value[‘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”: ‘ . $value[‘
stars ‘] . ‘,
“bestRating”: “5”,
“worstRating”: “1”
}
</script>
‘;
return $html;
}
add_shortcode(‘star_bar’, ‘make_star_bar’);Could you please help?
Thanks a lot!
February 26, 2023 at 2:15 pm #2547827Bernardas
Also, is there a way to add the star rating after the article title? Similar to here: https://www.gsmarena.com/xiaomi_13_pro-review-2535.php Or the only way is to create unique hooks for every post since the ratings would be different?
February 27, 2023 at 3:32 am #2548350David
StaffCustomer SupportIt looks like a spacing issue in that users code.
Try this instead:function make_star_bar( $atts ) { $value = shortcode_atts( array( 'stars' =>5, 'color' => '#f00' ),$atts); $stars = $value['stars']; $prefix = 'star-bar-'; $uniqueClass = uniqid($prefix); $percentage = 100 * $stars / 5; $html = '<span class="'.$uniqueClass.'">★★★★★</span> <style> .'.$uniqueClass.' { background: linear-gradient(90deg, '. $value['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": "' . $stars . '", "bestRating": "5", "worstRating": "1" } </script> '; return $html; } add_shortcode('star_bar', 'make_star_bar');February 27, 2023 at 9:11 am #2548881Bernardas
Works fine now. Thanks a lot, David!
Any smart way to implement the star rating after the article title, before the text? (for multiple pages)
-
AuthorPosts
- You must be logged in to reply to this topic.