The best way to learn about how the WordPress template files work together is to build your own theme. All of what follows is based on Putting It All Together from WordPress All-In-One For Dummies, 2017, 3rd Edition, pp 466 – 475.
What we’re going to do is build our own theme to see how template files fit together to work their magic. We’ll learn a lot doing this.
WP has built in functions to include the main template files (header.php, sidebar.php, and footer.php in other templates. An include fuction is a custom PHP function that is built into WordPress , allowing you to retrieve the content of another template file and display it along with content in another template file.
Here are examples of template files and include functions:
|Template File||Include Function|
|header.php||<?php get_header ( ); ?>|
|sidebar.php||<?php get_sidebar ( ); ?>|
|footer.php||<?php get_footer ( ); ?>|
|search.php||<?php get_search_form ( ); ?>|
|comments.php||<?php comments_template ( ); ?>|
If you want to include a file (in a template) that doesn’t have a built in include function (in WP), you need a different piece of code. To add a unique sidebar to a certain page template for example, you could name that sidebar file sidebar-page.php. To include that sidebar in another template, you use the following code:
<?php get_template_part( ‘sidebar’, ‘page’); ?>
In this statement, the PHP get_template_part function looks through the main theme folder for the sidebar_page.php file and displays the sidebar.
On pp 467 -471 the author shows us how to create a new WordPress theme by using some of the basic WordPress templates.
So first, after connecting to your web server via SFTP, you’re going to click on the wp-content folder and then click the themes folder
And in the themes folder create a new folder and call it, say, mytheme.
And then in your text editor, say notepad++, you’re going to save:
- Header template
- You create the file by typing in the lines of code on p 468 and then save the file with the file name header.php. (If you buy the book, the author gives you a link where you can download all of the code for all of the files we’re discussing in this section.
- Theme Functions:
- Type in the lines of code on p. 468 or download it and then save it using the filename functions.php.
- The theme functions file registers the widget area for your site so you’re able to add widgets to your sidebar by using the WP widgets available on the Widgets page of the Dashboard.
- Sidebar template:
- Create the file by typing in the code on pp 468 + 469 or by downloading it and then save it with the file name sidebar.php.
- This code tells WordPress where you want the WordPress widgets to display on the sidebar of your site. In this case, the code tells WP that widgets are displayed on the sidebar of your site.
- Footer template:
- Type in the lines of code on 469 or download it and save it and then save with the filename footer.php.
- Create the file by typing in the code on pp. 469 – 471 or download it and then save it with the filename style.css.
So with all these files in our WP site we need to create one last template file. We need to create the Main Index Template.
And we’ll do that in an upcoming post.