Breaking the code up into template files

Now that we've got the Loop working in our theme, it's time to start breaking the theme down into template files, which will help us make sure edits flow consistently across all of the various aspects of the theme.

A good rule of thumb for separating markup and code into its own template file is:

  • First and foremost, avoid duplicate markup and code
  • Secondly, you'll be able to create template files to address any unique markup and code that should only appear in special circumstances, like on a home page, but nowhere else

The most common template files we'll look at first are the header.php, footer.php, and sidebar.php template files. Each of these template files will be used in various types of pages on the site.

The header should be the same for a post or an archive as for a static page. The footer should remain consistent across all types of content on the site. If we update or change the header or footer, we'll want that change to appear everywhere it's used and not have to edit two or three different template files. The same goes for the sidebar—we may want it on several types of pages, or not. This is easy to manage if it's contained in its own file, and included where needed into other template files which control different content types.

Including everyone

These three template files (mentioned in the previous section) are so common that they have their own include template tags in WordPress, namely get_header, get_sidebar, get_footer. The tags are used like so in your template file:

  • <?php get_header(); ?>—to call the header.php file
  • <?php get_sidebar(); ?>—to call the sidebar.php file
  • <?php get_footer (); ?>—to call (yes, you've guessed it) the footer.php file

Let's start by creating the header.php file and adding the tag to include that in our index.php file.

Creating a header file

The header is more than just the beginning of the HTML5 file and the specific <head> tag. It includes the opening <body> tag and everything that is in the header of the theme's design—what is often referred to as the banner .

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset