Short story:
I’m trying to get the ID of the Header Image in WordPress.
All I found was this guide, which dosn’t seem to work anymore:http://nickohrn.com/2013/09/get-attachment-id-wordpress-header-image/
Long Story
I’m trying to make the WP Header responsive with an srcset. so I don’t want to use this code
<img id="masthead-bg" src="<?php header_image() ?>" alt="">
…but instead want to use the wp_get_attachment_image_srcset function to get the srcset of my header image. Only problem: I need an Image ID for this function -> The ID of my Header image.
<img id="masthead-bg"
src="<?php header_image() ?>"
srcset="<?php echo wp_get_attachment_image_srcset( image_id(), 'thumbnail' ); ?>"
sizes="100vw" alt="">
Any suggestions?
Try this…
Note: if you use a proper WordPress function to get the image it should add in all the srcset etc stuff for you, to allow for responsive images.
To answer the original question, I’ve found the simplest way to get the ID while I was filtering the markup that gets outputted by
<?php the_header_image_tag(); ?>
(introduced in v4.4).Responsive WordPress Header Image with fallback: