Support

Account

Home Forums ACF PRO How do I reuse an ACF custom Block inside of another ACF block template?

Unread

How do I reuse an ACF custom Block inside of another ACF block template?

  • I’ve currently got an ACF custom block that is being used around the site.
    Let’s call it InnerACFBlock, but would like to insert it into another ACF custom block on certain occasions let’s call that OuterACFBlock.

    I’ve been calling it using a function like this…

    /**
     * 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 su_risky_render_acf_block( $block_name, $attrs = array(), $post_id ) {
    	$block      = acf_get_block_type( $block_name );
    	$content    = '';
    	$is_preview = false;
    	
    	foreach ( $attrs as $attr => $val ) {
    		$block['data'][ $attr ] = $val;
    	}
    	$block['id'] = $post_id;
    
    	return acf_rendered_block( $block, $content, $is_preview );
    }

    Inside the OuterACFBlock I’m just getting the post data for the post that the OuterACFBlock is in..and passing that into this function to then render out the InnerACFBlock along with the block name etc.

    It is displaying my InnerACFBlock at present in the OuterACFBlock.. but not passing in things like URL params.

    Does anyone know of a better/ more efficient method to render out an ACF Custom Block inside of another ACF custom block (or inside of a template php file even)?

    Many Thanks.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.