I’m using a shortcode to pull in different loops via the loops-name.php. For some reason it is always at the top of the page. I googled it and using echo instead of return causes that problem but with my code I am not using echo. Here’s the shortcode:

// setup the shortcode for use
function friendly_loop_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'category' => '',
'module' => ''
), $atts ) );

include(locate_template('loop-'.$module.'.php'));
 }

Any idea why this is happening?

2

You can buffer the output like this:

ob_start();
include(locate_template('loop-'.$module.'.php'));
return ob_get_clean();

EDIT. I tried this, worked fine.

function friendly_loop_shortcode( $atts, $content = null ) {
     extract( shortcode_atts( array(
         'category' => '',
         'module' => ''
     ), $atts ) );

     ob_start();
     include(locate_template('loop-'.$module.'.php'));
     $output = ob_get_clean();
     //print $output; // debug
     return $output;
}

if (!is_admin()) {
     add_shortcode('test', 'friendly_loop_shortcode' );
}

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *