The has_header_image() WordPress PHP function checks whether a header image is set or not.
Usage
if (has_header_image()) { // Your custom code }
Parameters
- None
More information
See WordPress Developer Resources: has_header_image()
Related functions: get_header_image()
Examples
Display header image if it exists
If a header image is set, this example will display it using the img
HTML tag.
if (has_header_image()) { echo '<img src="' . get_header_image() . '" alt="Header image">'; }
Add a CSS class when header image is present
This example adds a CSS class to the body when a header image is set.
$body_class = has_header_image() ? 'has-header-image' : ''; echo '<body class="' . $body_class . '">';
Display alternative content when no header image is set
This example will display alternative content, like a site title, when no header image is set.
if (has_header_image()) { echo '<img src="' . get_header_image() . '" alt="Header image">'; } else { echo '<h1>' . get_bloginfo('name') . '</h1>'; }
Check for header image in a custom theme function
This example shows how to create a custom function in your theme’s functions.php
file to check if a header image is set.
function my_theme_has_header_image() { return has_header_image(); }
Apply custom styling to header when image is present
This example shows how to add custom inline CSS when a header image is set.
$header_style = has_header_image() ? 'background-image: url(' . get_header_image() . ');' : ''; echo '<header style="' . $header_style . '">';