Skip Navigation

[Resolved] Shortcode results appearing at top

This support ticket is created 7 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by davidR-12 7 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#546626

I have a page here:

hidden link

the modules that are listed at the top (the ones that are more spread out) are coming form a shortcode but the shortcode is actually below the modules below( that are closer together).

The PHP code is as follows and is used to display an ACF 'relationship' custom field:

add_shortcode('solutions_view', 'solutions_view_func');
function solutions_view_func(){
$posts = get_field('solutions_new_view');

if( $posts ): ?>
	
	<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
	    <div class="home-module-single">
              <div class="home-module-single-inner">
                  	    	<img src="<?php the_field('solutions-logo', $p->ID); ?>"</a>

                  <a href="<?php echo get_permalink( $p->ID ); ?>"><p><?php echo get_the_title( $p->ID ); ?></p>
	    </div>
</div>
	<?php endforeach; ?>
	
<?php endif;
}
return solutions_view;

How do I get the results to appear where the shortcode is placed?

Thanks,

David

#546653

I can't really offer much assistance because this code has no relationship to Toolset or Toolset's APIs. "the_field()" and "get_field()" are ACF API methods. But if I were you I would start debugging here, this looks unusual:

return solutions_view;

Typically shortcodes return a string, not the name of the shortcode...though it's possible this is a syntax I'm just not familiar with. Seems like you would be building a string up, then returning that string.
https://codex.wordpress.org/Shortcode_API

#546900

I found a solution for this using 'ob_start();' at the beginning and '$output = ob_get_clean();
return $output;' at the end:


function solutions_view_func(){ 
ob_start();
$posts = get_field('solutions_new_view');
if( $posts ):
	foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
   <a href="<?php echo get_permalink( $p->ID ); ?>"> 
	    <div class="home-module-single">
              <div class="home-module-single-inner">
                  
              <p><img src="<?php the_field('solutions-logo', $p->ID); ?>"</p>
            <p><?php echo get_the_title( $p->ID ); ?></p>
	    </div>
</div>
</a>
<?php	endforeach;
endif;
$output = ob_get_clean();
    return $output;
}
add_shortcode('solutions_view', 'solutions_view_func');