Using get_template_part() in a shortcode

Looking to use get_template_part() inside a WordPress shortcode? All you may need is to use PHP’s output buffering functions.

Output buffering to use get_template_part() in shortcodes

You simply need to call the ob_start() and ob_get_clean() functions around your get_template_part() function as per below:

function template_part_shortcode() {
     ob_start();
     get_template_part( 'example-template-part-name' );
     return ob_get_clean();
}
add_shortcode( 'exampleshortcode', 'template_part_shortcode' );