Partial and Miscellaneous Template Files

The header.php file does exactly what you would expect.  It contains all the code that the browser will render for the header. This is a partial template file because unless a different template file calls the template tagget_header(), the browser will not render the contents of this file.

Often sites have the same header regardless of the page or post you’re on.  However, some sites have slight variations such as a secondary navigation or different banner image depending on the page. Your header.php file can handle all these variations if you use conditional tags.

Almost all themes have a header.php file as the functionality and maintainability of this file pretty much demands its creation.

Below is an example of a header.php found in the twenty fifteen theme.

Expand full source code

Some of the code may look a little daunting at first, but if we break it down, it becomes simple enough. After the opening commment, the head is created. The template tag wp_head() pulls in all of our styles and any scripts that would appear in the head rather than the footer that we enqueued in our functions.php file.

Next, the body is opened and a mix of HTML and PHP are present. You can see some conditional tags in the site branding div that tweak a little bit of what is shown based on the page the user is on. Then the site navigation is pulled in. Lastly, the main site-content div is opened which will be closed most likely in the footer.php file.

One important template tag to note is body_class() found in the opening body tag. This is a super handy tag that makes styling your theme a lot easier by giving your body classes depending on the template file and other settings being used.