1

I am attempting to call a template part via a shortcode. Ive read all about how to do this over and over on this site and yet after having tried a number of things still am unable to call a simple php echo time string. The file name is test-one.php .Here is what I've tried:

function test_2( $attr ) {
    ob_start();
    get_template_part( 'test', 'one' );
    return ob_get_clean();
}

add_shortcode('test2', 'test_2');

function test_2( $attr ) {
    ob_start();
    get_template_part( 'test-one' );
    return ob_get_clean();
}
add_shortcode('test2', 'test_2');

function test_2( $attr ) {
    ob_start();
    get_template_part( 'wp-content/themes/theme-name/template-parts/test-one' );
    return ob_get_clean();
}

add_shortcode('test2', 'test_2');

Template part code is

<?php

echo "Current as of <br>  ".date("m-d-Y h:i:sa");

Seems like it should work, no? Where am I going wrong?

10
  • You shouldn't include wp-content in the parameters either, 'test', 'one' was correct. When you say that it doesn't work, can you be more specifiic? The shortcode is not added? The shortcode works but there is no output? PHP fatal error? It's unclear what it's doing if it's not doing what you expected, or how you're testing this
    – Tom J Nowell
    Aug 16, 2020 at 22:58
  • I also see you have ` the shortcode tag of res-1` is this a broken comment? Or text that's not meant to be in there? Your code as is would generate a PHP syntax error due to the missing // at the start of the comment
    – Tom J Nowell
    Aug 16, 2020 at 23:02
  • @tomJNowell Sorry left over from a comment I removed most of
    – Ted
    Aug 16, 2020 at 23:55
  • For 'test' 'one' without any path, it simply shows the name of the shortcode in the shortcode box, doesnt do anything. Yes page is published, theme doesnt matter, have switched that too.
    – Ted
    Aug 16, 2020 at 23:56
  • Then the contents of your shortcode are irrelevant, it isn't being registered, where is the code that registers your shortcode located? You're using [test2] not [test_2] right?
    – Tom J Nowell
    Aug 17, 2020 at 8:38

1 Answer 1

2

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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