Home › Forums › Front-end Issues › Use ACF to dynamically put and ID into a shortcode
Hello,
I am trying to use ACF to enter an ID for a download. I then want to display the download on the front end using a shortcode that has some other information in it.
So the result I want is this:
[download id="576" template="box-custom"]
the ACF is a sub field called download_id_number
I found this thread (http://support.advancedcustomfields.com/forums/topic/how-to-use-a-shortcode-with-acf/) which I thought might work but I’m not really sure what I’m doing!
I ended up with this:
<?php
$download_ids = get_sub_field('download_id_number');
$shortcode = '[download id="' . implode(',', $download_ids) . '" template="box-custom"]';
echo do_shortcode( $shortcode );
?>
I also tried this (assuming the implode bit I don’t need):
<?php
$download_ids = get_sub_field('download_id_number');
$shortcode = '[download id="' . echo $download_ids . '" template="box-custom"]';
echo do_shortcode( $shortcode );
?>
But it doesn’t work so obviously it’s not right for what I’m trying to achieve or just as likely I’ve just made a mistake!
Can anyone please advise?
Thanks!
Oh, and this code does appear within the correct repeater calls:
<?php while ( have_posts() ) : the_post(); ?>
<?php // check for rows (parent repeater)
if( have_rows('resource_downloads') ): ?>
<div class="section group sectionspace">
<?php // loop through rows (parent repeater)
while( have_rows('resource_downloads') ): the_row(); ?>
<div class="col span_4_of_12 txtcenter">
<?php
$download_ids = get_sub_field('download_id_number');
$shortcode = '[download id="' . echo $download_ids . '" template="box-custom"]';
echo do_shortcode( $shortcode );
?>
</div><!-- END span_4_of_12 -->
<?php endwhile; // while( has_sub_field('resource_downloads') ): ?>
</div><!-- END section group -->
<?php endif; // if( get_field('resource_downloads') ): ?>
<?php endwhile; // end of the loop. ?>
Got it working… much simpler!
<?php echo do_shortcode('[download id="'.get_sub_field('download_id_number').'" template="box-custom"]'); ?>
Glad you got it working. I was going to ask what type of field it was. The code you originally tried was for a field type that holds an array and in your case you have a field that holds a single value just in case you were looking for the reason why. You should mark your own answer as the solution to close out this topic.
The topic ‘Use ACF to dynamically put and ID into a shortcode’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.