When you create a WordPress theme, it's important to understand how a theme actually works. Rather than provide a single template for each individual scenario, WordPress uses a distributed templating system based on dry-development principles. To make sense of all this, let's first look at what happens when you enter an address to a WordPress post into your browser. So let's assume your WordPress site is set up with custom permalinks, so they have these nice, human-readable permalinks like mor10.com/test-drive.
The first thing that happens here is WordPress will translate that human-readable permalink into a machine-readible permalink. And you end up with mor10.com/?p=2726. And this last component here, p equals 2726, is what's important, because each entry in WordPress, whether it be a post, a page, a category, a menu item, or anything else, has a custom ID that you can target when you want to bring it into your page. Once you have the ID, it's sent to the database, and the database immediately checks what kind of content is this.
In the case of 2726, it's a post, and this information is sent back to the file server and the correct template is retrieved. For a post, that template is single.php, and you'd think that single.php would contain all the necessary code to display all the content, but that's not the case. In WordPress we have a distributed system, so single.php sets up the scaffolding for the post, and then calls in a bunch of other template files. So the content may be displayed by content.php or content-single.php.
The header is displayed by header.php, the sidebar by sidebar.php, and the footer by footer.php. And this means we can easily swap out one or all of these components without having to worry about any of the other ones. You can see the value of this when you then go to an index.php template. So this will be the front page, or any of the other blog index pages. Here we're using the same header.php, the same sidebar.php, and the same footer.php to display the overall content around.
But since we are now showing an index and we have to loop through multiple different posts, we keep running content.php over and over again, one time for each post. And that way we're reusing the same file over and over again instead of repeating the same code in the same template multiple different times. But WordPress is not just restricted to just posts. You also have pages and pages, like posts, have ID numbers. When you find a page you can then also apply a custom page template to that page, so pages can be displayed completely differently from other content.
You could, for example, choose to add a custom header. In that case, the file would be called something like header-custom and you could call it in to display in place of the regular header. You can also choose to display the regular footer, even though you have a custom header. And you could choose to eliminate the sidebar and just display the page content from content-page.php across the width of the entire screen. This distributed structure is what I referred to when I said that WordPress is based on DRY development principles.
DRY is an acronym that stands for Don't Repeat Yourself. And that's a good mantra to follow when you're writing code, because you don't want to repeat the same code over and over. Instead, you want to separate all repeatable elements into either separate files or separate functions, so that if you want to repeat the header in multiple different templates, then you just put it in a header file. If you want to repeat content several times in different templates, or maybe even several times within one template, then you put it in a content file.
If you want a custom header but you still want to be able to display the regular footer, then you split them out, so that you can call one or the other when you want to. And if you have a function within the content template, for instance, displaying the author name and the publishing date, and you know that you'll repeat that, also, in another content template, or somewhere else on the site, then you break that out into a separate function that is then called in and then used whenever necessary. Having a clear understanding of the distributed nature of WordPress themes allows you to quickly identify and navigate between different sections of your theme.
The main thing to remember here is that every major piece of content usually has its own template file. Sidebars are found in sidebar.php, headers in header.php, content in content.php, or a variety of different content files, and so on, and so on.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Your file was successfully uploaded.