Support

Account

Home Forums ACF PRO Using get_template_part in acf

Solved

Using get_template_part in acf

  • 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')); ?>
    
  • Hello @tmgale12

    Can you give me an example of what get_sub_field('icon') returns?

  • 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?

  • @genuino I know this is a relatively old thread. But there is a typo in @hube2’s code I guess. It should be get_template_part() not get_teplate_part(). Please note that the “m” is missing in the word “template”.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Using get_template_part in acf’ is closed to new replies.