Archived topic
Displaying ACF loop inside of Query loop
5 replies · Started by Jason on November 8, 2022
Hi y'all. Can someone just take a look at this code and tell me what I'm doing wrong? The goal is to get custom ACF fields to display inside of the query loop block. It outputs correct data, but it displays in a strange list outside of the grid container
function social_links_in_loop(){
if( have_rows('social_media') ):
echo '<div class="social">';
//echo '<h3>My Social Channels</h3>';
echo '<ul>';
while ( have_rows('social_media') ) : the_row();
$socialchannel = get_sub_field('social_channel');
$socialurl = get_sub_field('social_url');
echo '<li>';
echo '<a class="nav-link" rel="nofollow noopener noreferrer" href="' . $socialurl . '" target="_blank">';
echo '<i class="fa-brands fa-' . $socialchannel . '" aria-hidden="true"></i>';
echo '<span class="sr-only hidden">' . ucfirst($socialchannel) . '</span>';
echo '</a></li>';
endwhile;
echo '</ul>';
echo '</div>';
endif;
}
add_filter( 'render_block', function( $block_content, $block ) {
if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'social-links-loop' ) !== false ) {
$block_content = social_links_in_loop();
}
return $block_content;
}, 10, 2 );
Link to output included. Thank you, as always.
Hi Jason,
Have you tried using headline block or button block's dynamic data option to pull the ACF field directly?
And I don't see a block with social-links-loop class.
Hi Ying,
The social links are coming from an ACF repeater field with sub-fields so that I can loop over them and pull link and icon data. The built-in dynamic data functionality won't work. I've been able to make it work on archive pages and custom single posts using portable hook shortcode method, but to display in a query loop is different (can't use shortcode - as I understand it). Hence, my super messy method of trying to render a block with the above filter.
The class social-links-loop was attached to a container block, and I just attached it to a headline block. Still nothing showing inside the grid, but it's displaying outside the grid at the top of the page. The output of the icons correspond with the 3 featured artists social link data, so it's pulling the right info, just not displaying it in the right place. ??
Link included that displays the social links working correctly through the portable hook shortcode method. Thank you for your time!
(can’t use shortcode – as I understand it)
It is the case, but you can give render_block() with do_shortcode()a try, it usually would work.
For example:
add_filter( 'render_block', function( $block_content, $block ) {
if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'social-links-loop' ) !== false ) {
$block_content = do_shortcode('[your_shortcode]');
}
return $block_content;
}, 10, 2 );
Let me know!
Brilliant! Thank you, again! I had to add single quotes to the [your-shortcode] -> '[your-shortcode]' but it worked like a charm. You're a lifesaver!
I had to add single quotes to the [your-shortcode] -> ‘[your-shortcode]’
Oh you are right, sorry about that :)
Glad it works!