Last active
December 11, 2023 19:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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