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

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

  • 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?

  • Hi, I think @matthewfarlymn ‘s solution is what I am needing, but I am not 100% on how to make it work for what I need.
    I have a CPT – Events with two taxonomies (event_category) – upcoming & past events.
    I am trying to have them listed in order of the event date.
    For upcoming events, the one happening soonest first – to the furthest away.
    And for past, then the most recent first. Etc..
    The thing is, the event date is entered in the post via an ACF field within a block in each post.
    My current template only shows the posts if I remove or comment out the
    'meta_key' => array( $date ),
    Template File: GIST

    How can I use the solution above, and integrate it with the template?
    I tried dropping it into functions.php, but got an error (had edited to include block name & field).
    Any guidance would be greatly appreciated.

  • Normally, in my footer, I’d pull ACF fields from the Contact page. That post ID is 11. I decided to use ACF Gutenberg blocks instead. The block file is called business-info.blade.php. Therefore, business-info is my block name. Is it possible to parse that block from my Contact page into the footer?

    I thought this might work:

    `
    if ( function_exists( ‘get_field’ ) ) {
    $pid = get_post( 11 );
    if ( has_blocks( $pid_content ) ) {
    $blocks = parse_blocks( $pid->post_content );
    // my field called name
    $name = $block[‘attrs’][‘data’][‘name’];
    foreach ( $blocks as $block ) {
    if ( $block[‘blockName’] === ‘acf/business-info’ ) {
    echo $name;
    }
    }
    }
    }
    `

  • I also ran into this question and solved it with some modifications to the answer given earlier. Maybe it will help someone.

    function fetch_large_slider($postId) {
    	$slider_images = [];
    	$pid = (empty($postId) ? get_post() : $postId);
    
    	// check if acf is used
    	if (function_exists('get_field')) {
    		if (has_blocks($pid)) {
    			$blocks = parse_blocks( $pid->post_content );
    
    			// run trough acf gutenberg/acf-blocks and find the right one
    			foreach ( $blocks as $block ) {
    				// find all selected images
    				if ($block["blockName"] == 'acf/full-page-slider') {
    					// build new array from found images
    					$image_set = $block["attrs"]["data"]["fullpage_slider_images"];
    					// collect image urls inside an array
    					foreach ($image_set as $imageID) {
    						$slider_images[] = wp_get_attachment_image_src($imageID,'full',false)[0];
    					}
    				} else {
    					continue;
    				}
    			}
    		}
    	}
    	return $slider_images;
    }
  • Hi @ruurd, thanks for posting the solution.

    Where to add this code to fetch the blocks data on the home page?

    I have added it to functions.php file and used echo $slider_images. But it does not render any output on the page.

    We are trying to display the ACF block heading and title using on home page the above code as a reference point but it didn’t work for us.

    Any help would be appreciated..

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

The topic ‘ACF 5.8 – Parse Gutenberg blocks and get ACF data outside of post’ is closed to new replies.