WordPress Theme Development Tutorial Manual - Template File Inclusion
Includes thematic core documents
We already know that WordPress themes are built from many different template files. These files usually include sidebar.php
(math.) genus header.php
respond in singing footer.php
. These files are created using the Template TagsCalled , for example:
We can call custom versions of these files by customizing the file name sidebar-{your_custom_template}.php
(math.) genus header-{your_custom_template}.php
as well as footer-{your_custom_template}.php
. We can then include this file using the custom template name as the only argument to the call to the template function, as shown below:
get_header( 'your_custom_template' );
get_footer( 'your_custom_template' );
get_sidebar( 'your_custom_template' ).
WordPress creates pages by compiling various files. In addition to the standard files for headers, footers and sidebars, we can also use the get_template_part() to call template file snippets anywhere in the theme. To create a custom template file in your theme, assign an appropriate name to the file and use the same custom template system as the header, sidebar, and footer files, such as:
slug-template.php
For example, if we want to create a custom template to display published content, we can create a template named content.php
of the template file, and then create a self-customizing template snippet file by suffixing this filename, for example:content-product.php
. Then, we can load this template file in the theme as shown below:
get_template_part( 'content', 'product' );
If we wanted to make these template files more organized, for example, if we added separate template files for different article types, we could create a directory of content-templates and put the article type template snippets into that directory.
Theme hierarchy of the themes we use my-theme
It may be as follows. style.css
respond in singing page.php
Contained in context.
- themes
- my-theme
- content-php
- content-location.php
- content-product.php
- content-profile.php
- style.css
To include your content template, we need to add the catalog name to the slug
parameter as follows:
get_template_part( 'content-templates/content', 'location' );
get_template_part( 'content-templates/content', 'product' );;
get_template_part( 'content-templates/content', 'profile' );
Link to the topic directory
To link to the theme's directory, we can use the following function:
If we are not using a child theme, this function will return the full URI of the theme's main folder. we can use it to refer to subfolders and files in the theme, as shown below:
echo get_theme_file_uri( 'images/logo.png' );
If we are using a child theme, this function will return the URI of the file in the child theme, if it exists. If the file is not found in the child theme, this function will return the URI of the file in the parent theme. it is important to keep this in mind when distributing a theme or in any other situation where a child theme may or may not be active.
To access the paths to the files in the theme directory, you can use the following functions:
together with get_theme_file_uri() Likewise, this function will return the path to the file in the child topic, if it exists. If the file is not found in the child topic, this function will return the path to the file in the parent topic.
In a child theme, we can use the following functions to link to a file URI or path in the parent theme's directory:
together withget_theme_file_uri()
Likewise, we can refer to subfolders and files like this:
echo get_parent_theme_file_uri( 'images/logo.png' );
//or
echo get_parent_theme_file_path( 'images/logo.png' ); /or
Be careful when referencing files that may not exist, as these functions will return the URI or path to the file, regardless of whether the file exists or not. If the file is missing, these functions will return a broken link.
function (math.) get_theme_file_uri() (math.) genus get_theme_file_path() (math.) genus get_parent_theme_file_uri()(math.) genus get_parent_theme_file_path() was introduced in WordPress 4.7.
For previous versions of WordPress, use the get_template_directory_uri() (math.) genus get_template_directory()(math.) genus get_stylesheet_directory_uri()(math.) genus get_stylesheet_directory() The
Note that the functions in the newer 4.7 version run the older functions as part of the checking process anyway, so it makes sense to use the newer functions when possible.
Dynamic links in templates
Regardless of how our fixed link is set up, we can post the link to the page by referencing the post ID. Below:
<a href="/en/</?php echo get_permalink($ID); ?>">Here's a link</a>
This is a convenient way to create page menus because you can change the size of the page without breaking the links, since the IDs will remain the same. However, this may increase database queries.