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' );


  • Did you try it? – rudtek Oct 27 '17 at 17:37

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)


    get_template_part( 'page', 'example' );

    $content = ob_get_clean();

    return $content;


add_shortcode( 'custom', 'custom_code' );
  • I don't understand! Excuse i'm a newbie. I want use the shortcode....in your code where is the shortcode? – Antonio Carbone Oct 27 '17 at 17:32
  • 1
    he's assuming you left your shortcode line: add_shortcode( 'custom', 'custom_code' ); Now, if in a page, you use [custom]. it will call this function. – rudtek Oct 27 '17 at 17:39
  • 1
    rudtek is right, I improved my answer a little bit. – mmm Oct 27 '17 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.