Support

Account

Home Forums General Issues Inject ACF via shortcode and prevent duplication

Unread

Inject ACF via shortcode and prevent duplication

  • Hi all!

    I created a custom short code to inject ACFs (repeater field) into my content. Here is the template code which is saved under /shortcodes/generic/faq.php

    <?php if( have_rows('tab') ): $i=0; ?>
    
    	<aside class="faq">
    
    		<h2><?php the_field('title'); ?></h2>
    
    	<?php while( have_rows('tab') ): the_row(); 
    
    		$i++;
    		$label = get_sub_field('label');
    		$content = get_sub_field('content');
    
    		?>
    
    		<div class="tab">
    			<input id="tab-<?php echo $i; ?>" type="checkbox" name="tabs">
    
    			<?php if( $label ): ?>
    				<label for="tab-<?php echo $i; ?>"><?php echo $label; ?></label>
    			<?php endif; ?>
    
    			<?php if ( $content ): ?>
    				<div class="tab-content"><p><?php echo $content; ?></p></div>
    			<?php endif; ?>
    		</div>
    
    	<?php endwhile; ?>
    
    	</aside>
    
    <?php endif; ?>

    And here I add the short code via functions.php

    function faq_section($atts) {
      ob_start();
      get_template_part('shortcodes/generic/faq');
      return ob_get_clean();
    }
    add_shortcode('faq', 'faq_section');

    Now, when I populate the custom fields and add the value to my page content via [faq] it works like a charm. The annoying thing is that at the end of the page the ACF values are added again pulling the same template even.

    Any idea how I can prevent this from happening and only show the ACF values when I add the shortcode?

Viewing 1 post (of 1 total)

The topic ‘Inject ACF via shortcode and prevent duplication’ is closed to new replies.