Support

Account

Home Forums Gutenberg ACF 5.8 – Parse Gutenberg blocks and get ACF data outside of post

Solving

ACF 5.8 – Parse Gutenberg blocks and get ACF data outside of post

    • abiwab

    • March 15, 2019 at 3:35 am

    Hey guys !
    I’ve been playing around with the latest ACF 5.8.0 Beta 4 and I’m kinda stuck right know with an idea I had.

    My website has very “content heavy” pages using Gutenberg and ACF Blocks, and I’d like to let the end user navigate easily through that content. I’m using many ACF blocks that basically contain specific titles througout the post. My idea was to display a navigation menu on the right that gathers everyone of these titles (so that the user can click and jump to the section in the post).

    How can I parse through the post, get those specific ACF blocks data, and display them as a list ?

    I would also like to display this list outside of the loop (such as in a header menu etc…).

    I’ve seen @portalzine post on the forum who seems to have a great solution to my problem (https://portalzine.de/dev/php/advanced-custom-fields-get-gutenberg-blocks-data/), but it appears that it can only get a specific block, and not a bunch of similar blocks ?

    If anybody has a solution to this I’d be very grateful 🙂

    Thanks !

    • v3nt

    • October 24, 2019 at 2:33 am

    looking for same solution – did you get anywhere?

  • Are you looking for something like this? The below would give you the data of each block item on the page and you can filter through them based on blockName or other information to target only ACF blocks.

    if ( function_exists( 'get_field' ) ) {
    	$pid = get_post();
    	if ( has_blocks( $pid_content ) ) {
    		$blocks = parse_blocks( $pid->post_content );
    		foreach ( $blocks as $block ) {
    			var_dump( $block );	
    		}
    	}
    }

    Keep in mind if you’re using reusable blocks the information isn’t included and only the ref ID is. I’m currently trying to get the ACF data from a ref ID so I can limit something in my footer from displaying unless the block exists on the page.

  • My final solution to parse for reusable block data also…

    if ( function_exists( 'get_field' ) ) {
    	$pid = get_post();
    	if ( has_blocks( $pid_content ) ) {
    		$blocks = parse_blocks( $pid->post_content );
    		foreach ( $blocks as $block ) {
    			if ( $block['blockName'] === 'acf/your-block-name' ) {
    				// Access to block data
    			} elseif ( $block['blockName'] === 'core/block' ) {
    				$block_content = parse_blocks( get_post( $block['attrs']['ref'] )->post_content );
    				if ( $block_content[0]['blockName'] === 'acf/your-block-name' ) {
    					// Access to "some" block data
    				}
    			}
    		}
    	}
    }
  • Hey Matthew!
    Thanks for this. This looks like a possible answer to something I’m trying to do. Your function looks like it loops through blocks and if a given block exists then you can display content from it. I have an acf block ‘hero-block’ that I want to place outside of and before the content area. How would I place a specific block where I want it to go using this method?

  • I was able to partially answer my own question. This is slick btw. Well done!

    My other question is if we are parsing through blocks for a specific block to display elsewhere on a page how do we prevent the block from ALSO appearing within the content?

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.