Site logo

[Resolved] Displaying ACF loop inside of Query loop

Home Forums Support [Resolved] Displaying ACF loop inside of Query loop

Home Forums Support Displaying ACF loop inside of Query loop

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2406859
    Jason

    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.

    #2406913
    Ying
    Staff
    Customer Support

    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.

    #2406935
    Jason

    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!

    #2406960
    Ying
    Staff
    Customer Support

    (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!

    #2406971
    Jason

    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!

    #2406972
    Ying
    Staff
    Customer Support

    I had to add single quotes to the [your-shortcode] -> ‘[your-shortcode]’

    Oh you are right, sorry about that 🙂

    Glad it works!

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