I’m doing this in a child theme, fyi.
FORGET THE ABOVE… let’s start over….
I’m testing this on a MAMP setup and I have a default header set up that is shorter vertically. For the “home” header I have a taller one, and for mobile I have a narrower one. In the code below you’ll see I have them specified.
<?php $header_image = get_header_image();
if(is_home()) :
echo '<a href="' . esc_url( home_url( '/' ) ) . '"><img class="header-image" src="/wp-content/uploads/2016/01/header-default-1200x280.jpg" alt="" /></a>';
elseif ( ! empty( $header_image ) ) : ?>
<a href="<?php echo esc_url( home_url( '/' ) ); ?>"><img src="<?php echo esc_url( $header_image ); ?>" class="header-image" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" /></a>;
else (wp_is_mobile()) :
echo '<a href="' . esc_url( home_url( '/' ) ) . '"><img class="header-image" src="/wp-content/uploads/2016/01/header-mobile-600x140.jpg" alt="" /></a>';
<?php endif; ?>
When tested, I get no header when I click on the blog title link, so my code sort of works because the default header doesn’t show. On all other pages I get the default header which is correct, but I get some code displayed below the header of ; else (wp_is_mobile()) : echo '';
. That’s from the mobile coding.
Mobile isn’t showing up when I narrow the page to test. So obviously I’ve got something wrong. The example code I received was supposed to work in TwentyTwelve but it’s not.
Could someone see what I’m missing?
Made some adjustments after apparently copying the code wrong from the other thread I referenced.
This is getting me a different header for “home”. While all other pages are giving me the default header. I’m 2/3 the way there. Still trying to figure out the mobile part, because I’m getting the default header instead.
<?php $header_image = get_header_image();
if(is_home()) :
echo '<a href="' . esc_url( home_url( '/' ) ) . '"><img class="header-image" src="wp-content/uploads/2016/01/header-default-1200x280.jpg" alt="" /></a>';
elseif(wp_is_mobile()) :
echo '<a href="' . esc_url( home_url( '/' ) ) . '"><img class="header-image" src="wp-content/uploads/2016/01/header-mobile-600x140.jpg" alt="" /></a>';
elseif ( ! empty( $header_image ) ) : ?>
<a href="<?php echo esc_url( home_url( '/' ) ); ?>"><img src="<?php echo esc_url( $header_image ); ?>" class="header-image" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" /></a>
<?php endif; ?>
Solved.
Mistook that “wp_is_mobile” would work in the header. It doesn’t.
So I have the “front” and “inside” headers working, and I created an alternate version for mobile that works whenever the screen goes below 400, using CSS to swap them out.
So in the header.php file is this…
<div id="normal-header">
<?php $header_image = get_header_image();
if (is_page ( 'home-page' ) ) :
echo '<a href="' . esc_url( home_url( '/' ) ) . '"><img class="header-image" src="/wp-content/uploads/2016/01/Header-Full-setup-1.jpg" alt="" /></a>';
elseif ( ! empty( $header_image ) ) : ?>
<a href="<?php echo esc_url( home_url( '/' ) ); ?>"><img src="<?php echo esc_url( $header_image ); ?>" class="header-image" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" /></a>
<?php endif; ?>
</div> <!-- #normal-header -->
<div id="mobile-header">
<img src="/wp-content/uploads/2016/01/Header-Mobile.jpg" alt="" />
</div> <!-- #mobile-header -->
And in the CSS, goes this…
/* hide mobile header when over 400px */
#mobile-header {
display: none;
}
/* switch header displays at 400px */
@media screen and (max-width: 400px) {
#normal-header {
display: none;
}
#mobile-header {
display: block;
}
#mobile-header img {
width: 100%;
}
}