Skip to content

Instantly share code, notes, and snippets.

@lgladdy
Last active December 11, 2023 19:54
Show Gist options
  • Save lgladdy/7c83332eba10e6a5338fd298ed855f16 to your computer and use it in GitHub Desktop.
Save lgladdy/7c83332eba10e6a5338fd298ed855f16 to your computer and use it in GitHub Desktop.
This code sample enables support for the ACF shortcode inside a query loop by overriding the default shortcode block renderer with one which will inject the correct Post ID into the shortcode parameters
<?php
add_filter( 'render_block_core/shortcode', 'test_acf_shortcode_render', 10, 3);
function test_acf_shortcode_render( $content, $parsed_block, $block ) {
$content = preg_replace_callback( '/\[acf\s.*?\]/', 'acf_inject_query_loop_post_ID', $content );
return $content;
}
function acf_inject_query_loop_post_ID( $match ) {
return str_replace( '[acf', '[acf post_id="' . get_the_ID() . '"', $match[0] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment