Support

Account

Home Forums Gutenberg Include ACF Block from code

Solving

Include ACF Block from code

  • I was wondering if it is possible to reuse the acf block templates from code.

    e.g.
    – I have registered a block called ‘related posts’.
    – I can add this block in the Gutenberg editor and fill the fields
    – block_header (text)
    – amount_of_posts_to_show (number)

    Gutenberg now renders my block using my php template I registered and this is all working fine.

    But what I want is to add one of these blocks to my post-template (not block-template) via code

    e.g. something like this:

    
    <?php get_header(); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <h1><?php the_title(); ?></h1>
    <div><?php the_content(); ?></div>
    
    <?php 
    DO_MY_BLOCK(array(
    'blockType' => 'acf/my-block',
    'block_header' => 'This is a header',
    'amount_of_posts_to_show' => 3
    ));
    ?>
    
    <?php wp_reset_postdata(); ?>
    <?php endwhile; endif; ?>
    
    <?php get_footer(); ?> 
    

    On some pages the content writer might want some freedom regarding where this block should be placed, but on many pages I want this block to be placed by default, without the writer having to manually place them each time.

    Can this be done?

  • Hi,
    did you found a solution for this problem?
    Best regards,
    AndrΓ©

  • I’ve achieved this by hooking into the function that ACF uses to render blocks, which enqueues the correct JS/CSS and template you specified when you declared the block.

    You then need to manually pass in attributes that will be used in your template in place of the block’s custom fields (as these won’t be available, obviously!)

    
    // functions.php
    /**
     * Render an ACF block with your chosen attrs in place
     * Allows you to use the block within PHP templates
     * Note: Block template may need to be adjusted to read this data
     */
    function launchpad_render_acf_block( $block_name, $attrs = [] ) {
    	$block = acf_get_block_type( $block_name );
    	$content = '';
    	$is_preview = false;
    	
    	foreach( $attrs as $attr => $val) {
    		$block['data'][$attr] = $val;
    	}
    
    	echo acf_rendered_block( $block, $content, $is_preview );
    }
    

    Call the function in your template file and pass it an array of attributes:

    
    // archive.php
    launchpad_render_acf_block('acf/post-grid', ['block_post_grid_posts' => $grid_posts]);

    I’ve specifically placed the attributes in the same place that ACF stores custom field data within a block, but this doesn’t live update in admin so in your block template you should first look for the custom field data, followed by the $block['data'] eg:

    
    // block-template.php
    if( get_field('block_post_grid_posts') ) {
      $grid_posts = get_field('block_post_grid_posts');
    } elseif ( $block['data'] && $block['data']['block_post_grid_posts'] ) {
      $grid_posts = $block['data']['block_post_grid_posts'];
    }
    
  • Oh brilliant, looks good. I’ll try it in my next project. Thank you very much!

Viewing 4 posts - 1 through 4 (of 4 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.