I am trying to use this method to include a template part (HTML Code) rather than using some plain text. When I use text, it shows correctly below the content with the the_content filter. However, If I try to get_template_part, the content shows at the top of the Wordpress post rather than below the content.

Is there a better way to include/get template parts hooked to the before/after of single content in Wordpress?

Here is the code I am trying with the filter:

function educadme_book_fields_content_hook( $content ) {    
if( is_singular('books') ) {            
$beforecontent = '';
$aftercontent = get_template_part( 'parts/book', 'fields' );
$fullcontent = $beforecontent . $content . $aftercontent;
return $fullcontent;

Also tried:

$content .= get_template_part( 'parts/book', 'fields' );
return $content;

Still getting the same issue as all content of the get_template_part are shown above even the title, not just the_content. Thanks!

3 Answers 3


Be aware that a mix of return and echo from a function will change text positioning.

If you take this simple example :

function test(){
    $the_test = 'test return';
    echo 'test echo';
    return $the_test;

echo test();

Whatever the place of the echoed line, this line will be always print before echo test(); because echo is called before the return $the_test;

You need to play with get_template_part and/or template_redirect action to display your content like you want.

Hope your understand what I mean and the relation with your issue ;-)

  • Thanks that's an informative point but I found that I am not using any echo in this particular part. I have updated with code.
    – Dr.Hariri
    Jan 8, 2017 at 17:47
  • 1
    But a template part is echo not return, so everything is render before, like the example.
    – Benoti
    Jan 8, 2017 at 17:54
  • Oh I see. Thanks for explaining that. I am not a developer so that is something for me. Do I then need to change that to include a PHP file or is that also an echo? What do you recommend in my case to output something external? Thanks!
    – Dr.Hariri
    Jan 8, 2017 at 17:57
  • I just edit my answer. Hope you will find all element to make it works.
    – Benoti
    Jan 8, 2017 at 17:58
  • Thanks a lot. I decided to use ob_start to just throw everything in it to a variable and then use it directly. I hope this is reliable haha.
    – Dr.Hariri
    Jan 8, 2017 at 18:24

Ok since get_template_part and require couldn't be used since they echoed the content above the content and not below (Thanks @benoti), I decided to use a different approach:

ob_start(); ?>
<? global $post; ?>

Long HTML Content with some PHP variables inside to load all the custom fields & style I wanted instead of using an external file which I prepared before.

$book_fields = ob_get_clean();      
$fullcontent = $content . $book_fields;
return $fullcontent;

To avoid a "long HTML Content..." in the middle of your condition, another option is to include the "template part" but not with the get_template_part function.

Here is what I did after struggle on the same issue, and it works like a charm :

function add_post_switcher($content)
    if (is_singular('cp-portfolio')) {
        require get_stylesheet_directory() . '/template-parts/post-switcher.php';
        $post_switcher = ob_get_clean();
        $content .= $post_switcher;

    return $content;
add_filter('the_content', 'add_post_switcher', 1);

It's a old post, but hoppefully it'll help someone else :)

Your Answer

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

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