Is it possible to develop a function shortcode 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

1 Answer
1

get_template_part is echoing the HTML code. and the shortcode function must return the content.

then you can try that :

function custom_code($attr, $content, $tag)
{

    ob_start();

    get_template_part( 'page', 'example' );

    $content = ob_get_clean();


    return $content;

}

add_shortcode( 'custom', 'custom_code' );

Tags:

Leave a Reply

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