Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Star ranking shortcode not working as intented in my archive loop

  • I have implemented a shortcode, which can be found here, to display a 1-5 star rating based on a custom post_meta field created with ACF. I integrated this shortcode directly into my functions.php file by modifying the $rating = get_field(‘MY_ACF_CUSTOM_FIELD’) variable.

    In order to showcase the star rating in my archive query loop element, I included the shortcode [star_bar]. However, it appears that the dynamic value retrieved is always coming from the first post. Please check my screenshots for more context.

    What could be wrong?


  • Hi Jinigoe,

    Shortcodes will always return the get value of the page viewed on a Query Loop.

    You’ll need to filter it in.

    Can you try creating a Container Block and give it a class of: cu-star-placeholder. Adding Custom Classes:

    Then, add this snippet:

    add_filter( 'render_block', function( $block_content, $block ) {
        if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'cu-star-placeholder' ) !== false ) {
    		$rating_term = get_field('rating');
    		$star_color = 'orange';
    		$prefix = 'star-bar-';
    		$uniqueClass = uniqid($prefix);
    		$percentage = 100 * $rating_term / 5;
    		$html = '<strong>'.$rating_term.'/5</strong>
    		<span class="'.$uniqueClass.'">★★★★★</span>
    		.'.$uniqueClass.' {
    			background: linear-gradient(90deg, '. $star_color . ' ' . $percentage .'%, rgba(0,0,0,1) '. $percentage.'%);
    			color: rgba(0,0,0,0);
    			background-clip: text;
    			-webkit-background-clip: text;
    		<script type="application/ld+json">
    			  "@context": "",
    			  "@type": "Rating",
    			  "ratingValue": '.$rating_term.',
    			  "bestRating": "5",
    			  "worstRating": "1"
    		return $html;
        return $block_content;
    }, 10, 2 );

    Adding PHP:

  • Thank you so much! Adding a filter did the trick!

  • You’re welcome, Jinigoe!

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.