Last active
July 20, 2017 20:56
-
-
Save Xhynk/124fb3c17bda8492cb2821106c4d2b3b to your computer and use it in GitHub Desktop.
Set Featured Image as Background/Header on Cafe Pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'wp_head', 'trm_set_page_post_featured_image_as_header' ); | |
function trm_set_page_post_featured_image_as_header(){ | |
if( is_single() || is_page() ){ | |
if( has_post_thumbnail() ){ | |
$featured_image_url = get_the_post_thumbnail_url(); | |
echo '<!-- Feat. Img set in Appearance > Custom Functions --> | |
<style> | |
.front-page-header { | |
background-image: url( '. $featured_image_url .' ); | |
} | |
</style>'; | |
} | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Set the "Featured Image" as the header background on posts & pages | |
* | |
* @return: A `style` tag setting the header background image. | |
* @author: Alex Demchak | |
* | |
* @NOTE: | |
* • This is added to `wp_head` to give precendence to the style without | |
* resorting to heavy selectors, hacks, or !important. | |
* • Modify conditionals `is_single()`, `is_page()`, etc. if desired. | |
* • `if( has_post_thumbnail() ) {}` should remain as to not display | |
* blank background images on items without featured images. | |
* • The `.front-page-header` CSS selector will need to change based | |
* on whichever theme is active. | |
* • This is NOT Genesis specific, use anywhere! | |
*/ | |
## (\ /) | |
## ( . .) ♥ ~< Woofie Says: Don't Delete Me! > | |
## c(")(") | |
add_action( 'wp_head', 'trm_set_page_post_featured_image_as_header' ); | |
function trm_set_page_post_featured_image_as_header(){ | |
// Check to see if it's a single post/custom post type or page | |
if( is_single() || is_page() ){ | |
// Great, now see if a "Featured Image" has been set | |
if( has_post_thumbnail() ){ | |
// Set it as a variable, I don't like echoing in return context | |
$featured_image_url = get_the_post_thumbnail_url(); | |
// Finally write our little `style` tag. | |
echo '<!-- Featured Image - Set in Appearance > Custom Functions --> | |
<style> | |
.front-page-header { | |
background-image: url( '. $featured_image_url .' ); | |
} | |
</style>'; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment