Dynamically change GenerateBlocks QueryLoop
-
I`m trying to build testimonial blocks, and want to add stars equal to the ACF custom field. For now, I have this code:
$stars_value = get_post_meta(get_the_ID(), 'stars', true); // Maximum stars to display $max_stars = 5; for ($i = 1; $i <= $max_stars; $i++) { // Check if the current star $star_class = ($i <= $stars_value) ? 'filled' : 'empty'; echo '<svg xmlns="https://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="' . ($star_class === 'filled' ? '#FFC62B' : '#C4C4C4') . '"> <path d="M21.8346 9.07699L15.6611 8.63707C15.3727 8.61752 15.1332 8.44644 15.0257 8.1776L12.699 2.444C12.4497 1.833 11.5748 1.833 11.3255 2.444L8.9988 8.1776C8.89126 8.44644 8.65175 8.61752 8.36336 8.63707L2.18983 9.07699C1.52507 9.12587 1.25623 9.95194 1.76458 10.3821L6.50104 14.3658C6.72099 14.5515 6.81387 14.8302 6.74543 15.1137L5.2546 21.121C5.0933 21.7711 5.80205 22.2794 6.36906 21.9275L11.6188 18.6526C11.8632 18.501 12.1564 18.501 12.4057 18.6526L17.6554 21.9275C18.2224 22.2794 18.9263 21.7662 18.765 21.121L17.2742 15.1137C17.2057 14.8302 17.2937 14.5515 17.5186 14.3658L22.255 10.3821C22.7682 9.95194 22.4994 9.12587 21.8346 9.07699Z"/> </svg>'; }
which works correctly except for the fact that WP get_post_meta(get_the_ID(), ‘stars’, true); returns the current page ID, which is correct behavior, so I used render block to change the query loop block like this:
add_filter('render_block', function($block_content, $block) { if (!empty($block['attrs']['className']) && 'stars-holder' === $block['attrs']['className']) { $stars_value = get_post_meta(get_the_ID(), 'stars', true); // Maximum stars to display $max_stars = 5; for ($i = 1; $i <= $max_stars; $i++) { // Check if the current star $star_class = ($i <= $stars_value) ? 'filled' : 'empty'; // Modify the block content here $block_content .= '<svg xmlns="https://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="' . ($star_class === 'filled' ? '#FFC62B' : '#C4C4C4') . '"> <path d="M21.8346 9.07699L15.6611 8.63707C15.3727 8.61752 15.1332 8.44644 15.0257 8.1776L12.699 2.444C12.4497 1.833 11.5748 1.833 11.3255 2.444L8.9988 8.1776C8.89126 8.44644 8.65175 8.61752 8.36336 8.63707L2.18983 9.07699C1.52507 9.12587 1.25623 9.95194 1.76458 10.3821L6.50104 14.3658C6.72099 14.5515 6.81387 14.8302 6.74543 15.1137L5.2546 21.121C5.0933 21.7711 5.80205 22.2794 6.36906 21.9275L11.6188 18.6526C11.8632 18.501 12.1564 18.501 12.4057 18.6526L17.6554 21.9275C18.2224 22.2794 18.9263 21.7662 18.765 21.121L17.2742 15.1137C17.2057 14.8302 17.2937 14.5515 17.5186 14.3658L22.255 10.3821C22.7682 9.95194 22.4994 9.12587 21.8346 9.07699Z"/> </svg>'; } } return $block_content; }, 10, 2);
But it’s not working for some reason; perhaps you can help me identify my mistakes? Thanks.
The page I need help with: [log in to see the link]
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘Dynamically change GenerateBlocks QueryLoop’ is closed to new replies.