 Hey there, and welcome to Learn WordPress. Today you're going to learn all about templates in WordPress themes and why they're important for your WordPress site to function. In this video we will cover what WordPress theme templates are and what templates look like in block themes vs classic themes. First you will look at templates in block themes, learn about common block template features followed by a tour of the page.html template in the 2023 theme. Then you will learn about templates in classic themes, common classic template features followed by a tour of the page.php template in the 2021 theme. In WordPress, a template is a file that controls how your website's content will be displayed in a web browser. Templates draw information like page or post title, content, or custom fields from the database, or from other WordPress files, and generate the HTML code to be rendered in the browser. A single template usually controls an entire page, but it can also include one or more template parts that control just a section for that page. Template parts are reusable sections of code that achieve the same result when used in multiple templates. Templates and template parts can be of a high level of customization, or be extremely simple. Templates are the building blocks of a WordPress theme. Every theme has one or more templates, created to display specific content on your WordPress site. Depending on the theme you are using or developing, you can define as many or as few templates as you need. Getting acquainted with templates is an important step in learning to build themes from WordPress. There are two types of themes available in the WordPress theme repository, block themes and classic themes. Ever since WordPress included support for themes in version 1.5, theme templates have been PHP files that typically contain either PHP code or a mix of PHP code and HTML markup. Since WordPress 5.9, a new type of theme has become available. This type of theme makes extensive use of the block editor. The theme templates are HTML files and contain a mix of block markup and HTML markup. Differentiate between the two types of themes, the WordPress community has named themes that use the original PHP method for creating templates classic themes and themes that use the new block editor markup block themes. Regardless of whether the theme is a block theme or a classic theme, all template files follow the naming convention in the WordPress template hierarchy. Let's take a look at the template structure in block themes by viewing the template directory of the 2023 theme in a file browser. The first thing you'll notice is that block theme templates are located in a specific directory called templates. This new directory structure not only makes your theme folder more organized, but it's so easier to determine whether you're working with a template, template part, pattern or any other theme-related file. In the templates directory, you'll find the template files, which are all HTML files. Let's take a look at the markup in the page.html template of this theme in a code editor. Block templates only contain block markup and HTML markup. Block markup is identified by a specific HTML comments notation and tells the page being rendered to render the output for that block. Additional information about the block, like attributes or styling, is passed to the block markup in a JSON format. The HTML markup for a block exists inside the block markup and is used primarily for block structure. At the top of this template file is a template part block. In this instance, the template part being included is the header template part. When this page template is rendered, the block markup will find the header.html template part in the parts directory and include that part's block code. Next up is the start of a group block. Group blocks are used to group multiple blocks together. In this case, the group block has a tag name of main and has a custom margin set. It also opens an HTML main tag with a class of wp-block-group and applies the margin style to the tag. Next is another group block, which includes an HTML div tag. Inside the second group block is the post-featured-image block and the post-title block. Because a page in WordPress is a custom post type, these blocks will get the page featured image and title from the database respectively. Each of these blocks includes its own custom attributes and styling in the template code. Then the second block group is closed. After the second block group is the post-content block. This will get the content of the page from the database. Next is another template part block, which will include the comments.html template part. Then the main group block is closed. And finally another template part block, which includes the footer.html template part. Now let's take a look at the template structure in classic themes by viewing the template directory in the 2021 theme in a file browser. Here the templates are PHP files and are stored in the root of the theme directory. For example, you can see the index, page and single.php templates. You'll also notice that there are other files stored here, for example the header and footer PHP template parts. Let's take a look at the code inside the page.php template of this theme. In classic templates, you will typically see the use of template tags, which are PHP functions that instruct WordPress to do or get something. You will generally also see the use of the WordPress loop, which will gather information from the database. Let's break down the parts of the page.php template. It starts with an opening PHP tag, which identifies this as a PHP file so the server knows how to process it. Next there is a block of comments. Template comments are usually included at the top of the template file and describe its specific function. Right after the comments is the first template tag. This tells WordPress to insert all the information required for the header, including the code from the header.php template part, as well as any other relevant information. Next we have the implementation of the loop. The WordPress loop is another topic in itself, but can be described as using PHP code that displays WordPress database information for the current data being queried. It basically says, while there are posts available, then use these posts for whatever the code does next. After the loop, the template calls the get template part function, which gets the template part located at template parts slash content slash content hyphen page dot PHP. This template part contains the HTML and template tags to get the data for the page. Let's look at that template part. The template part starts with an HTML article tag. Article tag specifies independent self-contained content. In this case, the template tags the ID and post class set up the ID and class attributes of the article tag. Next up, there is a conditional check if this template is being rendered on the homepage or not. If it's not the front page, the HTML header tag is rendered, includes the template part slash header slash entry header template part, and calls the 2021 post thumbnail function, which generates a thumbnail for the page. If it is the front page and has a thumbnail, then just the HTML header tag is rendered and only the 2021 post thumbnail function is called to generate the thumbnail. After that is the main content container div for the page. Here, the the content function gets the page content and renders it. After that, the wp link pages function will render any page links for paginated content. This is in case the page content is using the next page quick tags in one or more places. Next is a section that renders a link to edit the page, which is only visible if an administrator is logged in. Last but not least, the article tag is closed. Back to the page template, after the content page.php template part has been included, the comments template function is called to include any comments on the page, but it will only include those comments if they are enabled and if there are comments for this page. The loop ends and finally the get footer function calls any footer template parts or footer information. And that wraps up this tutorial on templates in WordPress themes. For more information on developing WordPress themes, visit the WordPress developer hub at developer.wordpress.org and click on the develop themes link to visit the theme developer handbook. Happy coding!