Skip to main content
elaborated
Source Link
admcfajn
  • 1.3k
  • 2
  • 13
  • 30

You can also modify the shortcode to return a single string, the following should work as well:

function wporg_shortcodes_init(){
  function wporg_shortcode($atts = []) {
     extract( shortcode_atts( array(
        'term' => 'columns'
    ), $atts ) );

    $custom_taxonomy = $atts['term'];       

    $output = '';

    // add query of custom post type board_members      
    $recentPosts = new WP_Query
    (array('posts_per_page' => -1, 'post_type' => array('board_members'), 'columns' => $custom_taxonomy ));              
    while( $recentPosts->have_posts() ) :  $recentPosts->the_post();

        $output .= '<div class="b-thumb">'.the_post_thumbnail('thumbnail').'</div>';
        $output .= '<p class="b-info">';
          $output .= '<span class="b-name">'.the_title().'</span><br />';
          $output .= '<span class="b-country">'.the_field('country').'</span><br />';
          $output .= '<span class="b-position">'.the_field('position').'</span><br />';
          $output .= '<span class="b-content">'.printf(get_the_content()).'</span><br />';
        $output .= '</p>                ';

    endwhile;
        
    wp_reset_postdata();

    return $output;

  }
 add_shortcode('board-members', 'wporg_shortcode');
}
add_action('init', 'wporg_shortcodes_init');  

You can also modify the shortcode to return a single string, the following should work as well:

function wporg_shortcodes_init(){
  function wporg_shortcode($atts = []) {
     extract( shortcode_atts( array(
        'term' => 'columns'
    ), $atts ) );

    $custom_taxonomy = $atts['term'];       

    $output = '';

    // add query of custom post type board_members      
    $recentPosts = new WP_Query
    (array('posts_per_page' => -1, 'post_type' => array('board_members'), 'columns' => $custom_taxonomy ));              
    while( $recentPosts->have_posts() ) :  $recentPosts->the_post();

        $output .= '<div class="b-thumb">'.the_post_thumbnail('thumbnail').'</div>';
        $output .= '<p class="b-info">';
          $output .= '<span class="b-name">'.the_title().'</span><br />';
          $output .= '<span class="b-country">'.the_field('country').'</span><br />';
          $output .= '<span class="b-position">'.the_field('position').'</span><br />';
          $output .= '<span class="b-content">'.printf(get_the_content()).'</span><br />';
        $output .= '</p>                ';

    endwhile;
        
    wp_reset_postdata();

    return $output;

  }
 add_shortcode('board-members', 'wporg_shortcode');
}
add_action('init', 'wporg_shortcodes_init');  
Source Link
admcfajn
  • 1.3k
  • 2
  • 13
  • 30

Sounds like you need to wrap your shortcode contents in ob_start()and return ob_get_clean()

require_once(dirname(__FILE__).'/post-type.php'); //file that registers CPT

function wporg_shortcodes_init(){
function wporg_shortcode($atts = []) {
     extract( shortcode_atts( array(
        'term' => 'columns'
    ), $atts ) );

    $custom_taxonomy = $atts['term'];       

    ob_start(); // <- works like magic

    // add query of custom post type board_members      
            $recentPosts = new WP_Query
            (array('posts_per_page' => -1, 'post_type' => array('board_members'), 'columns' => $custom_taxonomy ));              
            while( $recentPosts->have_posts() ) :  $recentPosts->the_post();  ?>

                    <div class="b-thumb"><?php the_post_thumbnail('thumbnail') ?></div>
                    <p class="b-info">
                    <span class="b-name"><?php the_title(); ?></span><br />                                       
                    <span class="b-country"><?php the_field('country'); ?></span><br />
                    <span class="b-position"><?php the_field('position'); ?></span><br />
                    <span class="b-content"><?php printf(get_the_content()); ?></span><br />
                </p>                

                <?php wp_reset_postdata(); ?>         
        <?php endwhile; ?>      
   <?php        

   return ob_get_clean(); // <- more magic

    }
   add_shortcode('board-members', 'wporg_shortcode');
  }
 add_action('init', 'wporg_shortcodes_init');