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
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:
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
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');