Created
August 3, 2020 02:53
-
-
Save seothemes/d23af93a7f84ecbf433c7e55aed5a8ce to your computer and use it in GitHub Desktop.
Use inline image instead of background for hero sections
This file contains hidden or 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 | |
// Remove hero section backgrounds. | |
add_action( 'after_setup_theme', function () { | |
remove_theme_support( 'custom-header' ); | |
add_theme_support( 'custom-header', [ | |
'header-selector' => false, | |
'default-image' => CHILD_THEME_URI . '/assets/images/hero.jpg', | |
'width' => 1280, | |
'height' => 720, | |
'flex-height' => true, | |
'flex-width' => true, | |
'uploads' => true, | |
'video' => true, | |
'wp-head-callback' => '__return_false', | |
] ); | |
remove_action( 'genesis_before_content_sidebar_wrap', 'corporate_hero_section' ); | |
add_action( 'genesis_before_content_sidebar_wrap', function () { | |
ob_start(); | |
the_custom_header_markup(); | |
$custom_header = ob_get_clean(); | |
genesis_markup( [ | |
'open' => '<section %s>' . $custom_header . '<div class="wrap">', | |
'context' => 'hero-section', | |
] ); | |
do_action( 'corporate_hero_section' ); | |
genesis_markup( [ | |
'close' => '</div></section>', | |
'context' => 'hero-section', | |
] ); | |
} ); | |
add_filter( 'wp_get_custom_css', function ( $css ) { | |
return $css . '.wp-custom-header img{display:block}'; | |
} ); | |
add_filter( 'theme_mod_header_image', function ( $default ) { | |
$id = ''; | |
$url = ''; | |
if ( class_exists( 'WooCommerce' ) && is_shop() ) { | |
$id = wc_get_page_id( 'shop' ); | |
} elseif ( is_post_type_archive() ) { | |
$id = get_page_by_path( get_query_var( 'post_type' ) ); | |
$id = $id && $id->ID && has_post_thumbnail( $id->ID ) ? $id->ID : false; | |
} elseif ( is_category() ) { | |
$id = get_page_by_title( 'category-' . get_query_var( 'category_name' ), OBJECT, 'attachment' ); | |
} elseif ( is_tag() ) { | |
$id = get_page_by_title( 'tag-' . get_query_var( 'tag' ), OBJECT, 'attachment' ); | |
} elseif ( is_tax() ) { | |
$id = get_page_by_title( 'term-' . get_query_var( 'term' ), OBJECT, 'attachment' ); | |
} elseif ( is_front_page() ) { | |
$id = get_option( 'page_on_front' ); | |
} elseif ( 'posts' === get_option( 'show_on_front' ) && is_home() ) { | |
$id = get_option( 'page_for_posts' ); | |
} elseif ( is_search() ) { | |
$id = get_page_by_path( 'search' ); | |
} elseif ( is_404() ) { | |
$id = get_page_by_path( 'error' ); | |
} elseif ( is_singular() ) { | |
$id = get_the_id(); | |
} | |
if ( is_object( $id ) ) { | |
$url = wp_get_attachment_image_url( $id->ID, 'hero' ); | |
} elseif ( $id ) { | |
$url = get_the_post_thumbnail_url( $id, 'hero' ); | |
} | |
if ( ! $url ) { | |
$url = $default; | |
} | |
return $url; | |
} ); | |
}, 99 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment