What is the function of a WordPress template?

The function of a WordPress template is to generate how the pages will be displayed. The files used in the templates are collected from the WordPress MySQL database. It then generates the HTML code to deliver to the web browser. WordPress themes use as many template parts as required (as well as a style sheet).

Default templates are included in most WordPress themes. These are defined on WPBeginner as follows:

  • index.php – used to display the main page
  • header.php – used to display the header section
  • sidebar.php – used to display the sidebar section
  • footer.php – used to display the footer section
  • function.php – contains code and functions to be used in a theme
  • single.php – used to display the single blog post page
  • comments.php – used to display the┬ácomments and comment form.

Templates can have other templates inside them. For instance, a single.php (used displaying the for single post page), can also have other templates, such as header.php (to display the header section) or footer.php (to display the footer section).

Templates have a hierarchy, which means if a theme doesn’t contain one type of template, then it will use another, more general, template as a fallback. For example, if single.php is not included, index.php will usually take its place.