I have a domain name that is used to redirect to one of my blog pages. This greatly simplifies the creation of the web page for the domain name but also makes the page look a bit half-assed. The title of the page should be in the header image of the page, not the words “Dave’s Blog”. But how can this be changed?
After a quick search, it was clear that there would be a lot of barely useful information on the web. The very first blog entry that I found showed code that was obviously never tested because it would not work for anyone who tried it. What it did though was give me a hint about how to solve this on my own.
<?php
$templates = array();
$templates[0] = "header-{$post->post_name}.php";
$templates[1] = 'header-default.php';
print "<!-- " . $templates[0] . " -->\n";
load_template( locate_template( $templates, true ) );
?>
The code above is the contents of my WordPress header.php file in my theme directory. I am not sure how to customize this without using a custom theme so even my solution is not great. It is simple. The code creates an array of header file names. One is a name that is based on the slug of the current page and the other is header-default.php. The header-default.php file is just what was in header.php before these changes.
Now if there is a header file that is specific to the page slug, it is used. If not, the default is used. There are no tests for page names or titles and there is no hard coding of header file names of any sort. This is a fairly general purpose solution and this code was tested and worked for me.
Now that that has been described, the shortcomings of this solution are worth mentioning. An entire header.php file is needed for each different page. All I really want to to have a different image file. I suspect, but have not tried it, that I could use locate_template to look for image files too. I could then just modify the original header.php file to load an image that is specific to the page instead of loading an entire header.php file.