HEADER-FOOTER

HEADER-FOOTER will take you through the process of creating customized headers and footers for WordPress themes. Along the way, we will look at the way WordPress themes divide HTML pages into templates.

This project has the following steps:

Project Set-up. You can continue working with an existing project, such as the one you just created in The CHILD, or you can start a new site. If you start a new site, I recommend naming it n413_header. The instructions will assume you are using a new project. Set up a new database and do a fresh install of the Wordpress files, as explained in The INSTALL.

If you need the files for the twentytwenty theme, you can find them on the Resources page.

The Theme. Once you have a WordPress project ready to go, put some thought into which theme you want to use. There are a few considerations:

Themes vary greatly in the customizable features available in the header and footer. If your aim is to simply make changes to the header or footer, you may find that you need to look no farther than the Dashboard's Appearance > Customize menu. However, these same features make the code in the header.php and footer.php files much more complex, with several theme-specific functions for incorporating the user input. If your aim is to do something with customized code in the header or footer, it is best to work with a theme with minimal customization options.

The concepts presented here will apply to many themes you might work with, but the specific examples will be for Twenty Twenty and Twenty Twenty-One. However, the new "block" themes, such as Twenty Twenty-Two/Twenty Twenty-Three, will not work with the instructions here. Block themes have a very different structure than the classic PHP template-based themes.

It is possible to have a couple of themes going at once as you try things. You will need a child theme for each theme you want to use. However, you can activate and de-activate the child themes as you make changes to view your results.

header.php and footer.php. With your child theme set up, move header.php and footer.php into your child theme. Also move one of the page template files, such as single.php or singular.php. (The Twenty Twenty theme uses singular.php.)

Compare. Now, have a look at how the page markup is distributed among the template files. The page will be structured so the the main page content is enclosed in a "container", or <div> tag. Some themes use multiple nested tags for supporting structures like sidebars or post navigation. It might look like this:

<header>
...
</header>

<div id="content" class="site-content">
    <main id="main" class="site-main" role="main">
    ... <!-- Main page content -->
    </main><!-- #main -->
</div><!-- /#content -->

<footer>
...
</footer>

The theme designer will need to decide which template files the opening and closing container tags will be in. Some themes place the opening tags in the header.php file, with the closing tags in the footer.php file. Other themes will place both the opening and closing container tags in the main page template.

Let's look at Twenty Twenty-One first. The last few lines of header.php contain the following container markup tags:

twentytwentyone/header.php
...
<div id="content" class="site-content">
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

The first few lines of footer.php contain the corresponding closing tags for the container markup:

twentytwentyone/footer.php
			
        </main><!-- #main -->
    </div><!-- #primary -->
</div><!-- #content -->
...

This information tells you a few things:

  • You must leave these tags in place if you modify the header/footer templates.
  • This is not a good theme for adding extra structures, such as side bars, since any modifications will need to be done in the header/footer templates, and it will not be possible to add those structures to specific pages.
  • The theme's page templates (check single.php) will not contain the container markup.
  • If you should add a page template from another theme, you must adjust any markup in that template to match the structure you see here.

Now consider Twenty Twenty. You will not find the container markup in either header.php or footer.php. However, if you have a look at singular.php (the PHP template for rendering single post pages), you will find this:

twentytwenty/singular.php
<?php
/**
 * The template for displaying single posts and pages.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package WordPress
 * @subpackage Twenty_Twenty
 * @since Twenty Twenty 1.0
 */
get_header();
?>
<main id="site-content">
    <?php
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
            get_template_part( 'template-parts/content', get_post_type() );
        }
    }
	?>
</main><!-- #site-content -->
<?php get_template_part( 'template-parts/footer-menus-widgets' ); ?>
<?php get_footer(); ?>

The only container markup used for Twenty Twenty is in the page templates, and it consists of only a simple "main" container, which encloses "The Loop". Again, we learn some good information here:

  • The header/footer templates can be modified without being concerned about disturbing the page container markup.
  • It will be simple to add addtional container markup on specific templates if we need to add support for side bars, etc.
  • You must be careful not to disturb the container markup when you modify page templates.
  • If you bring in page templates from other themes, you must adjust the container markup to match the pattern on the other page templates in your theme.

Other themes may have variations of this pattern. For example, the popular theme "Neve" uses the <main> tag in the header/footer templates, but employs a system of "wrappers" that are used to create containers for various purposes.