Site logo

[Resolved] Star Rating System

Home Forums Support [Resolved] Star Rating System

Home Forums Support Star Rating System

  • This topic has 43 replies, 4 voices, and was last updated 3 years ago by Bernardas.
Viewing 15 posts - 16 through 30 (of 44 total)
  • Author
    Posts
  • #2036154
    David
    Staff
    Customer Support

    If 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/

    #2036635
    russel

    Works Great!

    but like you said there are no decimal value but i can live with that

    thank you

    #2036939
    David
    Staff
    Customer Support

    Awesome – glad to hear that!!

    #2435250
    Jasmin

    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.

    #2435454
    David
    Staff
    Customer Support

    Hi there,

    it depends 🙂 How are you using the shortcode – Is it inside a single post ? Or are you using it in the archives ?

    #2435459
    Jasmin

    Using it in a single post 🙂

    #2435511
    David
    Staff
    Customer Support
    #2435708
    Jasmin

    Awesome! It works perfectly. Thanks, David.

    #2436874
    David
    Staff
    Customer Support

    Glad to hear that!

    #2546941
    Bernardas

    For me, it’s always showing 5 empty stars no matter what number I add to the shortcode…

    #2547378
    David
    Staff
    Customer Support

    Hi there,

    if you can share a link to your site where the issue can be seen, we can take a look,

    #2547820
    Bernardas

    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&#8221;,
    “@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!

    #2547827
    Bernardas

    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?

    #2548350
    David
    Staff
    Customer Support

    It 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');
    #2548881
    Bernardas

    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)

Viewing 15 posts - 16 through 30 (of 44 total)
  • You must be logged in to reply to this topic.