header_image() returns null despite registered default header image
-
OBJECTIVE: load a separate header image for mobile agents for better performance
STRATEGY:
1. conditionally set the'default-image'
key of the$header_settings
array that is passed toadd_theme_support()
. Logs show that it is set successfully.
2. set the css background url by retrievingheader_image()
.I have the following in my theme setup in functions.php:
$ismobile = wp_is_mobile(); $themeroot = get_teplate_directory_uri(); $header_images = array( 'mobile' => array( 'url' => $themeroot . '/images/header-mobile.jpg', 'thumbnail_url' => $themeroot . '/images/header-mobile-thumbnail.jpg', 'description' => __( 'one', 'test' ) ), //array[0] 'desktop' => array( 'url' => $themeroot . '/images/header-desktop.jpg', 'thumbnail_url' => $themeroot . '/images/header-desktop-thumbnail.jpg', 'description' => __( 'two', 'test' ) ) //array[1] ); //$header_images register_default_headers($header_images); $header_settings = array( 'default-image' => $themeroot . '/images/header', // 'uploads' => true, 'width' => $ismobile? '299px' : '1696px', 'height' => $ismobile? '400px': '712px', 'default-text-color' => '000', 'flex-width' => true, 'flex-height' => true, ); //header_settings header_settings['default-image'] .= $ismobile? '-mobile.jpg' : '-desktop.jpg'; //log shows that default-image is set correctly add_theme_support('custom-header', $header_settings);
Now in my
header.php
I have the following at the end of thehead
section after the call towp_head()
:<?php if (has_header_image()) : ?> <style> header { background: url('<?php header_image(); ?>'); max-width: 100%; height: auto; } </style> <?php endif; ?>
has_header_image()
returns false, and even if I skip the condition,header_image()
returns null. It is possible to set the header image in the customizer, which loads the same image on desktop and mobile agents, but the default doesn’t work, although the value set fordefault-image
is correct.Thanks
- The topic ‘header_image() returns null despite registered default header image’ is closed to new replies.