Ylama Ylama - 28 days ago 5x
HTML Question

WordPress custom page templates reads from subdirectory

I need guides please, as I am new to this.

So I installed WordPress locally and managed to create my custom theme with just the basic theme content.

Theme Directory:


In this directory I wish to create sub-directories (
...), and then I want my pages to read from this sub-directories, ect.

The homepage will read from
with the home directory folder including files like

I did already manage to create a template file called
which I linked as a template Parent page to the new page I created in wordpress. My problem is the files in that directory do not get hooked.

Code from

/*Template Name: Home - Parent Page */

<?php get_header(); ?>
<div id="content-main">
<?php $contentSrc = "content/home"; ?>

window.contentSrc = '<?php echo $contentSrc; ?>';

<?php //get_template_part("page-home"); ?>
<?php get_footer(); ?>

This is something I've tried. And it then should hook the pages from sub directory content/home .

Code from:

<div id="home">
<div class="hook" data-hook="home-info"></div>
<div class="hook" data-hook="home-navi"></div>

This needs a function/js to work, but its just the idea of what I need and what I want to make happen.

Any links to guidelines for this or tips would be much appreciated, I have looked everywhere and i just do not find any easy or exact topic based on my question.

Maybe there is just a very easy way of doing it, i am interested.


One of the tools that WordPress offers is get_template_part();

However, this requires you do use Wordpress' loop on the page calling this function.

From the docs:

Includes the named template part for a theme or if a name is specified then a specialized part will be included. If the theme contains no {slug}.php file then no template will be included.

The template is included using require, not require_once, so you may include the same template part multiple times.

Using with theme subfolders

To use this function with subfolders in your theme directory, simply prepend the folder name before the slug. For example, if you have a folder called “partials” in your theme directory and a template part called “content-page.php” in that sub-folder, you would use get_template_part() like this:

<?php get_template_part( 'partials/content', 'page' ); ?>

Using PHP

You can also use a simple include()/require()/etc. With the following code:

<?php include( get_template_directory() . '/content/myfile.php'); ?>

This particular example uses the get_template_directory() function to find your custom theme path. An alternative (but still useful) function to use is get_stylesheet_directory()

Additional Resources