Create wp new header
Here is an example of how to create a new header in WordPress:
Step 1: Create a new PHP file
In your WordPress theme directory, create a new file called header.php
. This file will contain the HTML and PHP code for your new header.
Step 2: Add the header code
In the header.php
file, add the following code:
<?php
/*
Template Name: My New Header
*/
?>
<!DOCTYPE html>
<html <?php language_attributes();?>>
<head>
<meta charset="<?php bloginfo( 'charset' );?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php wp_title();?></title>
<?php wp_head();?>
</head>
<body <?php body_class();?>>
<header>
<!-- Your header content goes here -->
<div class="site-branding">
<h1><a rel="nofollow" target="_blank" href="<?php echo home_url();?>" rel="home"><?php bloginfo( 'name' );?></a></h1>
</div>
<nav id="site-navigation" class="main-navigation">
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) );?>
</nav>
</header>
<div id="content" class="site-content">
<!-- Your page content goes here -->
</div>
</body>
</html>
This code sets up a basic HTML structure for your header, including a <header>
element, a site branding area, and a navigation menu.
Step 3: Add the header to your theme
To use your new header, you'll need to add it to your theme's functions.php
file. Open the functions.php
file and add the following code:
function my_theme_header() {
get_template_part( 'header' );
}
add_action( 'wp_head', 'my_theme_header' );
This code tells WordPress to load the header.php
file when the wp_head
action is triggered.
Step 4: Activate the new header
To use your new header, go to the WordPress admin dashboard and navigate to Appearance > Themes. Click on the "Customize" button next to your theme's name, and then click on the "Header" tab. Select your new header from the dropdown menu.
That's it! Your new header should now be displayed on your WordPress site.