How to Create Different Headers in WordPress Site

Samuel Dawson 12th Jul 2020

WordPress is without a doubt one of the simplest content management system – that helps in achieving website creation and customization needs with minimal efforts. What's more? There are hundreds of thousands of WordPress plugins and themes over the web that makes it possible to incorporate several useful features and options in a website.

In essence, WordPress enables users to meet their website development and maintenance requirements with ease, even if they have little or no coding knowledge. Most importantly, WordPress is a highly flexible platform that renders ability to create custom solutions that meet specific needs of all sorts of niches.

And, in this post, I'll be talking about creating one such custom solution in WordPress. I will share the process of creating different headers in WordPress.

Use of Headers in WordPress Site: An Overview

WordPress users can make changes to their website headers or redesign it, so as to enhance the look and feel of the site. In fact, you can choose to add anything (like text, labels, etc.) within your WordPress site's header area that you want to show on the top of your WordPress page.

Every WordPress theme has a header.php file that contains code for the header part of a site. The code looks like:

<h1 id="header">
<a href="<?php echo get_bloginfo('url'); ?>">
<?php echo get_bloginfo('yourblog'); ?>
</a>
</h1>

In the above code, I've set up a header via an H1 template tag. And, I have passed two parameters in the template tag. The first parameter is the URL of a site, while the second one identifies the website name.

Understanding The Need For Creating Different Headers

Oftentimes, you may need to display separate headers on your site on per page basis, especially when you are running an e-commerce site wherein you need to showcase unique product listings on different pages. Here are a few key benefits of creating separate headers in a WordPress site:

  • Help Present Only Relevant Information To Users

Creating separate headers give you the ability to present information that is relevant to a specific page or post on your WordPress site. Let's suppose, you would like to show some particular information in the header of your blog page, such as the links of all blog posts. But you just wouldn't want to show the links to your blog in the header of your homepage or other inner pages.

  • Serves as an Effective Marketing Tool

As you know that header is the first thing that users will notice when they land on your website, adding discounts and hard-to-resist offers in your website header will help in grabbing the attention of your user's in a more efficient manner. However, you may only want to include your promotional offers within the headers of the main website pages, except for contact or any other page based on your preference. This can be achieved by having separate headers in different pages of your WordPress website.

How to Create Different Headers for WordPress Blog Posts?

Hope that you'll have come to know as to why you should focus on creating different headers for each page of your site. But, what if you would want to have different headers in your WordPress blog posts? You can achieve such a need, by following the below mentioned steps:

Step 1: The first thing you need is to create a new PHP file for your website header section, and assign it some name as you deem perfect. I will be creating a header file with the name “new-header-page.php”. This file will contain code for the header part of my theme.

Step 2: When you want to call any particular header, all you've to do is to open your theme's page.php template. And then, substitute the code for the header part in the page.php file with the following code snippet:

<?php

if(is_page('12')){

get_header('my-first-post'); // call header-my-first-post.php

}else{

get_header(); // call header.php

}
?>

Let us understand how the above code works:

A new custom header file is being created with the name "new-header-page.php". Next, if(is_page(12)) is used for defining '12' as the ID of the newly created header page - for which you need to create a different header.

In the next line of code, the default WordPress function get_header() function is used. This function helps make a call to the header.php template file that is present in your theme's directory.

Lastly, we have defined an alias name in the "get_header('first-post')" function, which is used to call the custom “new-header-page.php” header file.

Basically, the above code is telling WordPress that when the page named as "my-first-post" page is being opened by some user, then the file named as new-header-first-post.php will get executed if it exists. Or else, the default WordPress header.php file will be rendered.

Summing Up!

You may want to display different headers on multiple pages of your WordPress site. But, you may fear doing so as it seems complicated to you. However, in reality, implementing separate headers in a WP install is fairly easy, as you've seen in the post.

You just need to create a little piece of code and place it in within your custom header file, ensuring that the file will be executed when any user access the page wherein you would like to show a separate header section. I've just provided a simple example of how you can display separate headers on different WordPress pages, however, you can even choose to style the header based on your needs.

Authored By Samuel Dawson

Samuel Dawson is a technology enthusiast where he has been working as Front End Manager for Designs2html Ltd. He is a expert developer and involved in converting PSD to Wordpress theme at reasonable price. He has shared his views on how headers in Wordpress can be created.

Also on DiscussDesk