Antonio Carbone
Antonio Carbone

Reputation: 125

Create a function with get_template_part inside

Is it possible to create a function shortcode in wordpress with get_template_part() in functions.php ?

Something like this

function custom_code( $atts ){
        echo get_template_part( 'page', 'example' );
    }
 add_shortcode( 'custom', 'custom_code' );

thanks

Upvotes: 0

Views: 1222

Answers (1)

bueltge
bueltge

Reputation: 2402

It is possible, with a small function, that render the template and get back the code.

add_shortcode( 'my_shortcode', 'fb_get_template' );
function fb_get_template() {
    
    ob_start();
    get_template_part( 'my_template' );
    $content = ob_get_contents();
    ob_end_clean();
    
    return $content;
}

Upvotes: 3

Related Questions