Design a header and footer template for WordPress | Practical application (2) for developing a WordPress template

the previous lesson in the WordPress theme development series (Practical Application 1), we started by preparing the basic template file index.php, which is concerned with displaying the basic content of the template pages. In today’s lesson from the Developing WordPress Templates from Scratch series, we will explain how to create a custom header and footer for our WordPress template.

We have explained that calling the get_header() function in any Template file tells WordPress to include the header file header.php, and calling the get_footer() function in any Template file tells WordPress to include the footer.php file.

In the index.php file, we called these two functions, but we relied on the default header and footer for a backup template provided to us by WordPress, and today we will work on customizing the header and footer and designing them according to the requirements of our template.

Create a template header file header.php

Create a new file called header.php within your template folder. Within this file, we will write the necessary codes to fetch the required header content according to the design of our template.

This file includes all the codes that are called before starting to display the main content of the page within the <main> tag. It usually includes (the <head> tag, the opening <body> tag, and the <header> tag, which will collect the necessary part of the content in the header of each page, which appears before the actual content. per page)

As we explained in the template network diagram that we included in the previous lesson, our template must display within the <header> both the site logo on the left and the main navigation menu on the right. Before that, we also need to display a set of basic codes in the head tag, as we will explain in detail in our following paragraphs. .

1. Add basic data to pages

Before starting to fetch the data to be displayed in the site header, there are some important things that we must include in the <head> tag, which is the part that does not appear on the page, such as tags and basic HTML structural elements such as Doctype, which tells the browser how to interpret HTML, and the charset that you use on the site or Blog, and adjusts the viewport width to the width of the device you are using

It should be noted that here we have given the HTML tag class=”no-js”, and this is a way for developers to design pages with or without JavaScript codes. By setting this row, only CSS code can be used to design non-JavaScript pages. Thus, the developer can specify CSS formats for browsers that support running JavaScript code and other formats for browsers that disable it. Meaning that the no-js class will not be implemented unless JavaScript is disabled for the browser.

We must also call the <title> tag, which displays the site title at the top of the browser window, and write a set of metadata that includes identifying information such as the title of the document at the top of the browser and in search engine results, and places the necessary links to the format files and scripts that we use on the site.

Finally, we prefer to call the wp_head action hook in case the template or one of the plugins needs to add some code later.

The beginning of the file will look like this:

<?php
/**
* Header template file
* That is, everything above the main tag
* @package ola1
* @subpackage olatheme
*/
?>
<!DOCTYPE html>
<html class=“no-js” lang=“<?php bloginfo(“language“); ?>”>
<head>
<!— Basic Page Needs —>
<title>
<?php bloginfo(“name”); ?>
</title>
<!– Meta —>
<!– mobile specific meta —>
<meta name=“viewport” content=“width=device-width, initial-scale=1.0, maximum-scale=1”>
<meta name=“description” content=“Blog wordpress theme”>>
<meta name=“author” content=“OLA SALEH”>
<meta charset=“utf-8”>
<!– CSS —>
<link rel=“stylesheet” href=“<?php echo get_template_directory_uri();?>/style.css”>
<link rel=“shortcut icon” href=“images/logo.png” sizes=“100×100” >
<!– Header hook —>
<?php wp_head(); ?>
</head>

2. Display the website logo

Then we write the <body> tag and the <header tag to begin displaying the actual header data, which is the site logo and then the main navigation menu.

In our template, I will place a fixed logo image logo.png inside the images folder of our template and display it statically within the code, but it is important that I provide the site owner with a way to change the site logo and add his own logo whenever he wants, and the easiest way to achieve this is through the function website at any time.

The easiest way to achieve this is through the add_theme_support function that we explained in a previous lesson, to which we need to pass the name of the required feature custom-logo. We can also customize the dimensions of the logo by writing the following code in the template functions file functions.php   and it will be called automatically when the template is executed.

// Support the logo customization feature in the template
add_theme_support( ‘custom-logo’, array(
‘height’ => 100,
‘width’ => 100,
‘flex-height’ => true,
‘flex-width’ => true,
) );

This code will tell WordPress that our theme supports a custom, downloadable logo and make it provide a section where you can change the logo and upload any image you want under Dashboard>Appearance>Customize>Site Identity as follows.

By clicking on the Change Logo button, we can upload an image to display as the site logo, and the image size suggested here is 100 x 100 pixels. But here it is preferable to download a version with twice these dimensions that will appear perfectly on screens that support the Retina Display feature.

Simply adding a logo image here is not enough to make WordPress display it automatically. Why? Because it does not know where you want to display it, so you have to call the_custom_logo() function in the location where you want to display this logo within the header.php file.

This function generates HTML code and a link that directs the site visitor to the home page. The image source is the URL of the uploaded logo image.

The codes required to do this task in the header.php file are as follows:

<header class=“site-header”>
<div class= “wrapper site-header__wrapper”>
<!– Website logo —>
<div class= ‘logo’>
<?php
if ( function_exists( ‘the_custom_logo’ ) ) {
the_custom_logo();
}
else{
?>
<a class=“logo” href=“<?php echo esc_url( home_url( ‘/’ ) ); ?>” title=“<?php echo esc_attr( get_bloginfo( ‘name’, ‘display’ ) ); ?>” rel=“home”>
<img src=“<?php echo get_template_directory_uri(); ?>/images/logo.png” alt=“Logo” width=“100” height=“100” />
</a>
<?php } ?>
</div>

3.Display navigation menu

After displaying the logo, you must display the main navigation menu in the site header, but before creating the special navigation menu, create some pages on the site so that you can add them to the menu when you create it.

To do this, go back to the functions.php file, add menu support for this template inside the file, and register the desired menu display positions with the register_nav_menus() function.

If you go back to the initial design of our home page you will notice that it displays two navigation menus. One in the header and one in the footer. So, in our template, we will add a main menu in the header and a menu in the footer as follows:

// Register a main navigation menu and a footer navigation menu in the template
function register_my_menus() {
register_nav_menus([
‘primary’ = > __ ( ‘main menu’ ) ,
‘footer’ = > __ ( ‘footer list’ ) ,
]);
}
add_action( ‘init’, ‘register_my_menus’ );

Read more:  Create navigation menus in a WordPress theme and customize them with code

This code will cause two places to display menus within our template, and in these places the site owner can create any custom menu and specify the place he wants to display it.

After this writing, if you return to the control panel and choose the menus appearance, you will find these two places. If you create your menus, choose the display location for them in one of the places you registered, and then browse the site, you will not see these menus because you also did not tell WordPress where you will display them.

You must tell WordPress that you want to display the main navigation menu in your site’s header by calling the wp_nav_menu() function in the header.php file under the <nav> tag as follows:

<nav class=“header-menu-init-xl”>
<?php
if ( has_nav_menu( ‘primary’ ) ) {
wp_nav_menu( array(
‘theme_location’ => ‘primary’,
‘items_wrap’ => ‘<ul>%3$s</ul>’,
‘menu_class’ => ‘my-navigation’,
‘fallback_cb’ => false,
‘depth’=> 1,)
);
} ?>
</nav>

 

Now go back to the browser and view the home page of your site. The menu should now appear as follows:

You may be wondering why the list appeared like this? The answer is because WordPress creates a ul menu tag and a li tag for each of its elements by default. WordPress also adds a set of default formatting classes, whether for the menu or other page elements, based on several factors to help you format your site.

With this, we have added the required elements to the header of our template, and in the next step we must add the formats so that the header appears as desired.

4. Formatting the header elements (logo and menu)

As we just mentioned, WordPress adds several default formatting classes for elements, and if you wish, you can use these classes to design your web page.

Alternatively, if you do not want to use these automatically generated categories, you can work on adding your own categories and adding the necessary formats to them.

In this paragraph, we will add a design to the site’s header elements based on our own classes. So we will edit the style.css template file and add the following styles to it:

/* Header styles*/
.site-header__wrapper {
display: flex;
justify-content: space-between;
align-items: center;
}
div. logo
{ float: right;}
Div. m_nav
{float: left;}
.header-menu-init-xl
{ display: initial !important; }
header .logo img {
flex-grow: 0;
max-width: 100%;
margin-right: 20px;
}
header nav ul {
align-items: flex-start;
display: flex;
gap: 2rem;
list-style-type: none;
padding: 0;
margin-left: 20px;
font-weight:700;
}
header nav ul li a {
color: inherit;
text-decoration: none;
}

With this, we have finished the site header, and it will look like this after formatting:

Create template footer file footer.php

Now we will add the required elements to the footer of our WordPress template. Our template is simple and standard, and it includes all the code necessary to display it after the <main> tag.

In our template’s footer, we will display two elements: the footer list and the copyright text. We will also call the wp_footer() action hook to allow any subsequent modifications.

So we create a new file in the template folder and call it footer.php and add the following code to it:

<?php
/**
* Footer template file
* That is, everything that exists after the main tag
* @package ola1
* @subpackage olatheme
*/
?>
<footer>
<div class=“container”>
<!– 1.footer nav —>
<div class=“footer-nav”>
<section>
<?php
if ( has_nav_menu( ‘footer’ ) ) {
wp_nav_menu (
array(
‘theme_location’ => ‘footer’,
‘items_wrap’ => ‘<ul class=”inline”>%3$s</ul>’,
‘fallback_cb’ => false,
‘depth’ => 1,
)
);
}
?>
</section>
</div>
</footer>
<!– 2.copyright>
<div class=“site-info”>
<p class=“site-info”>
< ?php printf ( ‘%s.All rights reserved© %s’ , get_bloginfo ( ‘name’ ) , date ( ‘Y’ ) ) ; ? >
</p>
</div>
</div>
<!– 3.footer hook —>
<?php echo wp_footer(); ?>
</body>
</html>

The final step is to format the footer elements by adding the following formats to the style.css file

/* Footer styles*/
footer {
background: #553c9a;
color: white;
margin-top: 5rem;
padding: 2rem 1rem 2rem 1rem;
}
footer .footer-nav {
display: flex;
flex-wrap: wrap;
max-width: 1080px;
margin: 0 auto;
gap: 1rem;
justify-content: space-between;
}
footer .footer-nav section ul {
margin: 0;
padding: 0;
list-style-type: none;
}
footer .footer-nav section ul.inline {
display: flex;
gap: 2rem;
}
footer .footer-nav section ul li {
font-weight:400;
margin-bottom: 1rem;
}
footer .footer-nav section ul li a {
text-decoration: none;
color: white;
}
footer .footer-nav section ul li a:hover {
text-decoration: underline;
}
div.site-info{
background-color: #c1afd4;
padding: 2rem;
}
p.site-info {
font-weight:400;
text-align: center;
}
.site-info a:hover {
color: #553c9a; /* link color on hover */
}

Now, if we view our website in the browser, the footer will appear as follows:

The home page of our website will appear after customizing the header and footer as follows:

Conclusion

In today’s article, we learned how to create a custom header and footer in a WordPress template and how to format them to match the general look required for our template design. In the next lessons in the WordPress template development series, we will begin by customizing the rest of the site’s pages so that they display content different from the content displayed on the home page.

Avatar photo
I am a young man who has been working in WordPress and e-marketing for 10 years. I would like to share my experience with you so that we can become professional in WordPress I will be happy to share the experience with you.