Skip to content

Instantly share code, notes, and snippets.

@jameskoster
Last active August 15, 2023 14:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jameskoster/40a75071cd693b7282e62fe0e7f357da to your computer and use it in GitHub Desktop.
Display featured image as header background
add_action( 'wp_head', 'jk_header_background', 999 );
function jk_header_background() {
global $post;
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
if ( $post_thumbnail_id ) {
?>
<style>
// Replace '.my-header' with your header class.
.my-header {
background: url(<?php echo $post_thumbnail_url ?>);
background-size: cover;
}
</style>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment