Use a Featured Image as header image - wordpress

Here i want to make my wordpress pages with Different header Image.
Every Page Should have a Different Header Image.

So the Best Way to Do that is set header image from Featured Image of Page.



For that we have to get the image URL of Featured image of Page.

So here is the Code that will help you to get src of the featured image.

Paste this code in Your Header.php file where you want to display the featured Image.






<?php if (has_post_thumbnail( $post->ID ) : ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div id="custom-bg" style="background-image: url('<?php echo $image; ?>')">

</div>
<?php endif; ?>



Here  i am using my featured image as a my header <div> background Image.

So, The src of the image will stored on $image Variable..

Comments