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 14 posts - 31 through 44 (of 44 total)
  • Author
    Posts
  • #2548930
    David
    Staff
    Customer Support

    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.

    #2549220
    Bernardas

    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).

    #2549676
    David
    Staff
    Customer Support

    Did you remove that other function ? And clear any caches ?

    #2549713
    Bernardas
    #2550244
    David
    Staff
    Customer Support

    What i mean is, do you still have the other snippets saved anywhere ?

    #2550497
    Bernardas

    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

    #2551146
    David
    Staff
    Customer Support

    I tested the steps i added here:

    https://generatepress.com/forums/topic/star-rating-system/page/3/#post-2548930

    And this my ACF Field:

    2023-03-01_11-08-30

    And that works as intended using the [star_bar] shortcode.

    So i am not sure why it won’t work for you.

    #2551287
    Bernardas

    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…

    #2551362
    David
    Staff
    Customer Support

    My ACF Field Group has is set to Display on ALL posts:

    2023-03-01_14-15-20

    then in the single Post editor, i added the value in the ACF field.

    #2551598
    Bernardas

    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…

    #2551627
    David
    Staff
    Customer Support

    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.

    #2562917
    Bernardas

    Hey David, are you still available for help?

    #2562926
    David
    Staff
    Customer Support

    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.

    #2562947
    Bernardas

    The new topic with private info created.

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