Template part inside shortcode, unexpected reult

The get_template_part() function doesn’t assume any directory name, you have to explicitly supply it. It’s also relative to the active theme directory. So in many of your examples it was looking for the template in the root of your active theme. The correct format would be:

get_template_part( 'template-parts/test-one' );

If you wanted to, you could rename the folder to inc or includes – it’s arbitrary. You just need to reflect this change in the function call.


On a side note, WordPress does allow you to pass a 2nd parameter and will look for files joined by a dash. For example, this will also work:

get_template_part( 'template-parts/test', 'one' );

The benefit of this is it allows you to have multiple “types” of a specific part. You may have a content-post.php file to output your post content and a content-page.php to output your page content. Then in your theme you could call your template by:

get_template_part( 'template-parts/content', $post->post_type );

Just allows it to be a bit more dynamic.


Another side note. Any time you have trouble with these kinds of functions you can always use:

add_action( 'init', function() {
    $template = locate_template( $template_string_here );
    printf( '<pre>%s</pre>', print_r( $template, 1 ) );
    die( 'end' );
} );

This will output the template path WordPress is trying to access the template by.