听起来你需要将你的短代码内容包装在ob_start()
和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\');
您还可以修改快捷码以返回单个字符串,以下操作也可以:
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\');