Using Templates in WordPress

In themes:

get_template_part( 'slug-name', 'name' );
We should keep our templates inside template-parts directory.
So lets say our template file is in:
themes/theme-name/template-parts/home/home-my-template.php
To include this template you will write ( for e.g. inside front-page.php )

get_template_part( 'template-parts/home/home', 'my-template' );

For e.g. our template file is in:
themes/theme-name/template-parts/home/my-template.php

get_template_part( 'template-parts/home', 'my-template' );

In Plugin,

For e.g. our template file is in:
plugins/plugin-name/templates/my-template.php

load_template( PLUGIN_DIR_PATH . '/templates/my-template.php' );

If we want to pass data to the template, we do it like so :

set_query_var( 'name', 'Imran' );
load_template( PLUGIN_DIR_PATH . '/templates/my-template.php' );

Now we can access this variable in template file as $name.

Leave a Reply