SOLVED: Insert template shortcode into template dynamically/programatically from custom field

I have some templates that I want to insert in multiple locations, but I want to specify which template to use via a custom field on the post via ACF (Advanced Custom Fields).

So for example on blog post A, B and F, I want to display a Bricks template with ID 456 after my post content.

How do I get Bricks to render the template, using the shortcode string extracted from a custom field?

So my post has a text custom field with value:
[bricks_template id=“456”]
I can’t figure out how to get it to render the content of the template in my post.

I’ve extracted this custom field value into my post template:

  • with a basic text field containing that field as dynamic data {acf_post_cta_shortcode}
  • using PHP <?php echo do_shortcode( ' {acf_post_cta_shortcode} ' ); ?>
  • using more complex PHP <?php $cta_template=get_field('acf_post_cta_shortcode' ); echo do_shortcode( '$cta_template' ); ?>

and none of these approaches displays the content of the template, just the value of the field (i.e. [bricks_template id=“456”]) .

Note: I can successfully insert and render the template with a code element containing:
<?php echo do_shortcode( '[bricks_template id="456"]' ); ?>

What am I missing here and how can I get the dynamically chosen CTA template to render within my post template?

Thanks.

Never mind, I figured it out, using the more complex PHP version.

Posting the answer here for others.

I put the following in a code element (with execute code switched on):

<?php $cta_template = get_field( 'post_cta_shortcode' ); echo do_shortcode( $cta_template ); ?>

There were two issues with my previous attempts:

  1. I had the wrong field name for the get_field function (needed to drop the acf_ prefix)
  2. I needed to remove the single quotes around the variable name

the lazy people use condition :slight_smile:

Sorry, but that’s not what I was looking to do. I know how to use conditions, but this was about pulling a template shortcode string from an ACF field to display on the page.

I have at least 5, and more like 10+ of these various templates I want to choose from, so using conditions just isn’t practical.

In future, I’ll probably make the content of these CTA templates pull from a CPT, so I can use one template and instead store a specific flag for the CTA CPT content I want to display on each post.

But for now I have it working the way I want.

Cheers.

1 Like

$args = array(
‘post_status’ => ‘publish’,
‘post_type’ => 'jobs,
‘posts_per_page’ => 2,
‘order’ => ‘DESC’,
‘s’ => ‘’,
);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
echo do_shortcode(“[bricks_template id=”(id_template)“]”);
wp_reset_postdata();
}
}
sorry i have quesion my code here. I have a question. I’ve written a query with the conditions mentioned, and in the template, I’ve used a heading widget with content {post_title}. However, when executing the query, my template does not receive the title from the query.