Using WordPress get_footer function to create multiple footers WP website

📂 Category: PHP

🖺 Last modified: 7th Dec 2021

💻 Experience: Intermediate

🕑 Read Time: 5 min

In this tutorial we’ll analyze the case when you need different footers on different sections of your WP website. You’ll learn how the WordPress get_footer function works in order to start createing your own multiple footers websites. This tutorial is intended for intermediate experienced developers already comfortable with editing WordPress template files.

Reasons to create a multiple footers WordPress website

Since you’re going trough this WordPress get_footer function tutorial, you must already have an idea of the benefits of a multiple footers website. Multiple footers offer extensive functional and/or visual possibilities and website enhancements. Footer changes can vary from simple variations to complex structure changes depending of each project.

Multiple footers can be used to show different information on different parts of a website or to show or hide some footer elements. For example, the homepage can display a big footer containing a lot of elements, but sub pages can display smaller footers containing only essential information. Blog post footers can contain sharing buttons, unnecessary on static pages. Landing pages can have their own specific footer layouts. From the visual standpoint, the website footer can change it’s layout, design and color when browsing trough different sections of a website. All those possibilities are easily achievable once you master the WordPress get_footer function.

WordPress get_footer function explained

There are various cases and web design projects when multiple footers can significantly improve the website functionality and enhance the user experience.


The WordPress get_footer function in template files

In most cases, WordPress website file structure hierarchy consists of main template files and secondary template parts. Template files load template parts and form the complete web page. In our example, the WordPress get_footer function located inside one of the template files loads the footer template part and shows the footer of the website.

The usual main WordPress template files are the following:

  • index.php loads template parts for the homepage
  • single.php loads template parts for blog posts
  • page.php loads template parts that form sub pages
  • search.php s loads template parts that form the search results page
  • archive.php or category.php structure the post archives and category pages

As mentioned earlier, each of those template files contain the WordPress get_footer function that loads the default footer of the website. The get_footer WordPress function is commonly located at the very end of each template file. The default function syntax is get_footer(); with empty brackets (no parameters) and it will load and display the content of the footer.php template part.


Creating a multiple header WordPress website using the get_footer function

Now that we explained how the WordPress get_footer function works, it’s time to create our multiple footer website. The example website will display a different footer on it’s homepage, the sub pages, and accordingly on the blog posts.

Let’s say we want to display the default theme footer on the homepage. Not much to do in this case, we’ll leave the index.php file as it is and let it behave as we explained earlier. The get_footer() function inside index.php without additional parameters inside the brackets, will load and display the default footer.php file.


Adding parameters to the get_footer() WordPress function

To add parameters to the get_footer() WordPress function, firstly we have to create additional footer template parts and customize them. Afterwards, we’ll use the parameters inside the function brackets to load each custom footer template part within different template files.

Creating custom footer template parts is pretty simple. You have to locate and duplicate the footer.php file and rename it by adding a custom parameter to the file name. In order to do so, you have to open your WordPress theme folder using the cPanel File manager, a FTP Client or a File Management Plugin.

The following simple steps will guide you guide you trough the process of creating additional custom footer template parts:

  • access your website’s theme folder using a File manager of your choice
  • duplicate the file footer.php two times (for the purpose of this tutorial), or as many times needed for your project
  • rename the duplicated footer.php files by adding a parameter to the file name (for example: footer-single.php and footer-page.php)
  • edit the newly created footer template parts and customize them according to your project needs

Adding custom footers to template files using the WordPress get_footer function

As the first step of creating a multiple footer WP website is completed, now we have to to add the newly created custom footers to the main template files. As mentioned before, in this example we’ll use the default theme footer on the homepage. We’ll leave the WordPress get_footer function inside the index.php template file without a parameter inside the brackets. To make the post and page template files load their own custom footers, we have to add parameters inside the function brackets in those templates.

  • open the single.php file using the WordPress theme editor
  • locate the WordPress get_footer(); function on the end of the file
  • add the parameter you used when renaming the footer.php file inside the get_footer() function brackets
  • the modified get_footer PHP code should now look like get_footer(‘single’);
  • modify the page.php file accordingly by adding the page parameter inside the function brackets get_header(‘page’);


Multiple footers in one template file

Now that you learned how to create different footers on various sections of your website, here’s another good practice. You can add multiple get_footer() functions to a single main template file. For example, let’s create a footer that will display a contact form only on the website homepage. The sub pages and posts will have the same footer, only without the form.

Firstly we need to create one custom footer template part that will contain the contact form. Following the procedure described earlier, create a template part file and call it footer-contact.php.

To achieve the goal of this example, now you have to edit only the main index template index.php that structures the homepage. Locate the default WordPress get_footer function and add another get_footer function right before the default one. Add the parameter contact inside the brackets of the first get_footer() function, like this: get_footer(‘contact’); and save the changes.

With this little tweak, the website homepage will firstly load and display the custom footer template part containing the contact form. The default theme footer will be displayed right after it. Since we haven’t made any changes to other template files except index.php, all other parts oft the website will display only the default theme footer. A wide variety of possibilities opens up by combining custom WordPress get_footer functions on different sections of a website.


How to create a multiple header website?

Using a procedure quite similar to what we described in this tutorial, you can also create multiple headers for different sections of your website. Our Multiple header WordPress tutorial will guide you trough the process in details and help you to furthermore enhance your website.