Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active July 12, 2018 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/eb97dbf6fac537d2d8cd9b9d3d1a4e03 to your computer and use it in GitHub Desktop.
Save billerickson/eb97dbf6fac537d2d8cd9b9d3d1a4e03 to your computer and use it in GitHub Desktop.
jQuery(function($){
// Preload header image
var $header = $( '.page-header' ),
headerImage = $header.data( 'image' );
if ( headerImage ) {
var img = new Image();
img.src = headerImage;
$ (window ).load( function(){
$header.css( 'background-image', 'url(' + headerImage + ')' );
});
}
});
<?php
/**
* Page Header
*
*/
function ea_page_header() {
$image_id = $title = $content = false;
if( is_singular() && has_post_thumbnail() )
$image_id = get_post_thumbnail_id();
if( is_page() ) {
$title = get_the_title();
remove_action( 'tha_entry_top', 'ea_entry_title' );
}
if( is_home() ) {
$title = get_the_title( get_option( 'page_for_posts' ) );
}
if( is_archive() ) {
$title = get_the_archive_title();
}
if( is_search() )
$title = 'Search results for: <em>' . get_search_query() . '</em>';
if( is_home() || is_archive() || is_search() )
$image_id = get_post_thumbnail_id( get_option( 'page_for_posts' ) );
if( empty( $image_id ) )
$image_id = ea_cf( 'ea_header_image', false, array( 'type' => 'theme_option' ) );
echo '<div class="page-header" data-image="' . wp_get_attachment_image_url( $image_id, 'full' ) . '"><div class="wrap">';
if( !empty( $title ) )
echo '<h1>' . $title . '</h1>';
echo $content;
echo '</div></div>';
}
add_action( 'tha_header_after', 'ea_page_header' );
.page-header {
background-image: url(../images/loading.png);
background-size: cover;
background-position: center center;
position: relative;
transition: background-image 0.4s ease-in-out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment