Site logo

[Support request] How to add ACF Star rating in loop

Home Forums Support [Support request] How to add ACF Star rating in loop

Home Forums Support How to add ACF Star rating in loop

Viewing 7 posts - 16 through 22 (of 22 total)
  • Author
    Posts
  • #2530292
    Martin

    That’s the query loop. Feel free to log in to the site. Thanks!

    #2530885
    Fernando
    Customer Support

    Hi Martin,

    The login link you shared isn’t working.

    Can you check if it’s correct?

    #2530963
    Martin

    They are working for me. I have attached another user login details. Thanks!

    #2531218
    David
    Staff
    Customer Support

    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.

    #2531520
    Martin

    Still nothing, unfortunately.

    Are you able to login?

    Thanks

    #2531670
    Martin

    Or I might found it now! Thanks

    #2531671
    David
    Staff
    Customer Support

    So what is this?

    2023-02-13_14-54-05

    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.

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