Skip Navigation

[Résolu] Display ACF repeater fields via shortcode issue

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 2 réponses, has 2 voix.

Last updated by joshD-2 Il y a 6 années et 2 mois.

Assigned support staff: Sumit.

Auteur
Publications
#328856

Hello,

I'm trying to include an ACF repeater field via shortcode (included in functions.php in a Content template like so:


/*
*  ACF repeater shortcode
*/
            
function corridor_1_fn() {

if( function_exists('have_rows') ) {

if( have_rows('upper_tra_table') ): 

	echo 

	"<table class=''>
        <tr>
            <th>Status</th>
	    <th>Quality</th>
	    <th>Material</th>
	</tr>";

?>

	<?php while( have_rows('upper_cabinet_table') ): the_row(); ?>
        <tr>
            <td class="">
                <?php the_sub_field('upper_tra_status'); ?>
            </td>
            <td class="">
                <?php the_sub_field('upper_tra_quality'); ?>
            </td>
            <td class="">
                <?php the_sub_field('upper_tra_material'); ?>
            </td>
        </tr>

	<?php endwhile; ?>

    </table>

<?php endif;

}

}

add_shortcode( 'corridor_1', 'corridor_1_fn' );

I've called this shortcode within a template nested within several divs:

<div class="col l12 m12 s12">
      	[corridor_1]
</div>

For some reason this is working correctly but instead of displaying the table within the div element I've included it in, it is outputting at the top of the page, before the content template content?

I have no idea why and I wish I did

Thanks a lot in advance!

#328869

Sumit
Supporter

Hi,

Thank you for contacting support forum.

Shortcode should always return the value instead of printing.
As per the WordPress documentation
"Any string returned (not echoed) by the shortcode handler will be inserted into the post body in place of the shortcode itself."
Ref: https://codex.wordpress.org/Shortcode_API

Please consider this code. I am placing PHP buffer before content printing and at the end I am returning the content. It should work.

/*
*  ACF repeater shortcode
*/
             
function corridor_1_fn() {
 
if( function_exists('have_rows') ) {
    
    ob_start();
 
    if( have_rows('upper_tra_table') ): 

        echo

        "<table class=''>
            <tr>
                <th>Status</th>
            <th>Quality</th>
            <th>Material</th>
        </tr>";

    ?>

        <?php while( have_rows('upper_cabinet_table') ): the_row(); ?>
            <tr>
                <td class="">
                    <?php the_sub_field('upper_tra_status'); ?>
                </td>
                <td class="">
                    <?php the_sub_field('upper_tra_quality'); ?>
                </td>
                <td class="">
                    <?php the_sub_field('upper_tra_material'); ?>
                </td>
            </tr>

        <?php endwhile; ?>

        </table>

    <?php endif;
    
    $content = ob_get_contents();
    ob_end_clean();
    return $content;

    }
 
}

Thanks

#328988

Thank you for the clear, concise explanation. I was spending hours trying to get it to work, and it does now thanks to you!