I have the following file structure (simplified):

— app
  — header.php
  - views
    - header-default.php

In header.php, I'm using get_template_part as follows:

<?php get_template_part( 'app/views/header/header', 'default' ); ?>

This works, but suppose in the future I need to change my folder structure or simply rename the "app" folder. It won't work anymore. How do I make get_template_part always relative to the file I'm using it in? I tried:

<?php get_template_part( __DIR__ . '/views/header/header', 'default' ); ?>

but it's not working. I guess Wordpress always uses the root directory as a starting point to interpret the path. What options do I have?

  • Yes, get_template_part() works from the root of your active theme directory – Jaed Mosharraf Oct 11 '17 at 10:40

There is only one function get_template_directory() in wordpress, which can give you path of your theme. But there is no any function which give directory path of folder inside your theme.

There are two way to get the directory path of your template dynamic, and whenever your folder structure changed, you just need to update at only one place:

1) You can create your custom config variable to store the template directory path and whenever you want to include your template, you can get path from config variable and include it:

in wp-config.php:

<?php define('TEMPLATE_DIR_PATH','path to your template path'); ?>

and in your file(in which you want to include template) you can include as:

<?php get_template_part( TEMPLATE_DIR_PATH.'/header', 'default' ); ?>

2) Another way is to create custom function in function.php file of your active theme which will return the path of your template directory:

in function.php

function get_template_file_directory_path(){
        return "path to your template path"

and in your file(in which you want to include template) you can include as:

<?php get_template_part( get_template_file_directory_path().'/header', 'default' ); ?>

Hope this will help you.


This function searches for template by path inside the child theme (if used) and main theme. Since plugins and themes depend on it, you can't change the way it works.

The only solution is to write your own function and make it load templates the way you want it, relative to current template. And, then you use your function instead.

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.