So while developing this site, one of the challenges I faced was creating custom headers for multiple page layouts and templates. I could’ve created new page templates for each layout, but that would’ve quickly gotten bloated. Instead, I opted to use conditionals in functions.php to serve up custom HTML code in the header block, depending on what page layout or template is being called.
The code below is based off Geoffrey Rickaby’s How To: Create a Static Header in Genesis tutorial.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** Custom header */ remove_action('genesis_header', 'genesis_do_header'); remove_action('genesis_header', 'genesis_header_markup_open', 5); remove_action('genesis_header', 'genesis_header_markup_close', 15); function child_custom_header() { if (is_front_page()) { ?> YOUR HTML HERE <?php } elseif (is_page() || is_search() || is_archive() || is_home()) { ?> YOUR HTML HERE <?php } elseif (is_single()) { ?> YOURHTML HERE <?php }} add_action('genesis_before', 'child_custom_header'); |
/** Custom header */
remove_action('genesis_header', 'genesis_do_header');
remove_action('genesis_header', 'genesis_header_markup_open', 5);
remove_action('genesis_header', 'genesis_header_markup_close', 15);
function child_custom_header() {
if (is_front_page()) { ?>
YOUR HTML HERE
<?php }
elseif (is_page() || is_search() || is_archive() || is_home()) { ?>
YOUR HTML HERE
<?php }
elseif (is_single()) { ?>
YOURHTML HERE
<?php }}
add_action('genesis_before', 'child_custom_header'); You can find out more about PHP logical operators and WordPress conditional tags.
If you found this article helpful or need some help deploying the code, sound off in the comments below!
Looks good. I would recommend a few small changes though. You are adding the Primary menu in, so you should include the remove_action for the primary menu otherwise you will get two primary menus.
I would also prefer that the custom_header function have a prefix such as child_custom_header or my_custom_header ... Generally the prefix would be the child theme prefix. The reason for this is to make it less likely for the code to break when a plugin developer uses a common phrase like "custom_header" as a function name instead of prefixing themselves.
You also appear to be missing the <?php } before your elseif statements.
- spam
- offensive
- disagree
- off topic
Like