I want to be able to select a ‘template part’ in a repeater field.
I store animated svgs in a folder and I want to use them at the beginning of each section.
So far I have tried to input get_template_part('svg/inline','scales.svg');
into a text field called icon (stored as $icon), and then echo it out in the section of the HTML that I would normally have put get_template_part('svg/inline','scales.svg');
. This has not worked.
Can someone point me in the right direction? My code is below.
if( have_rows('sections') ):
while ( have_rows('sections') ) : the_row();
$title = get_sub_field('title');
$section = get_sub_field('section');
$icon = get_sub_field('icon'); ?>
<div class = "gen_section" id = "section_<?php echo $title; ?>">
<div class = "wrap">
<!--Need get_template_part below ($icon)-->
<span class = "icon_span"><?php if ( $icon ) { echo $icon; } ?>
<h2 class = "section-title"> <?php echo $title; ?> </h2>
</span>
<?php echo $section; ?>
</div>
</div> <?php
endwhile;
else :
// no rows found
endif; ?>
You are trying to run PHP code but echoed code is not run.
I would suggest altering your input to just include the second part of the shortcode, for example you would enter scales.svg
into the field.
Then in your template you would do
<?php get_teplate_part('svg/inline', get_field('icon_field')); ?>
Thank’s John, that’s worked.
My working code is below for reference.
// check if the repeater field has rows of data
if( have_rows('sections') ):
while ( have_rows('sections') ) : the_row();
$title = get_sub_field('title');
$section = get_sub_field('section'); ?>
<div class = "gen_section" id = "section_<?php echo $title; ?>">
<div class = "wrap">
<!--Need get_template_part here ($icon)-->
<span class = "icon_span"><?php if ( $icon ) { get_template_part('svg/inline', get_sub_field('icon')); } ?>
<h2 class = "section-title"> <?php echo $title; ?> </h2>
</span>
<?php echo $section; ?>
</div>
</div> <?php
endwhile;
else :
// no rows found
endif; ?>
I’m using ACF 5 but this solution doesn’t work for me:
get_teplate_part('parts/content', get_sub_field('tipo_blocco'));
tipo_blocco is a select field that if i put into the page i can see:
echo get_sub_field('tipo_blocco');
I’ve also tried to put the filed into a variable like this:
$tipo_blocco = get_sub_field('tipo_blocco');
get_teplate_part('parts/content', $tipo_blocco));
Where is the mistake?
The topic ‘Using get_template_part in acf’ 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.