Skip to content

Instantly share code, notes, and snippets.

@hiranthi
Created December 7, 2017 14:02
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 hiranthi/14bb153eb8b55d5ffd4027af0d7d0290 to your computer and use it in GitHub Desktop.
Save hiranthi/14bb153eb8b55d5ffd4027af0d7d0290 to your computer and use it in GitHub Desktop.
WordPress image_sizes voor responsive header
<?php
add_action( 'init', 'onx_register_image_sizes' );
function onx_register_image_sizes()
{
add_image_size( 'header_screen_large', 2000, 1200, array( 'center', 'center' ), true );
add_image_size( 'header_screen', 991, 700, array( 'center', 'center' ), true );
add_image_size( 'header_tablet', 767, 550, array( 'center', 'center' ) );
add_image_size( 'header_mobile', 479, 400, array( 'center', 'center' ) );
} // end onx_register_image_sizes
<?php
$custom_header = get_custom_header();
if ( ! empty( $custom_header->attachment_id ) ) {
$mobile_src = wp_get_attachment_image_src( $custom_header->attachment_id, 'header_mobile' );
$mobile = $mobile_src[0];
$tablet_src = wp_get_attachment_image_src( $custom_header->attachment_id, 'header_tablet' );
$tablet = $tablet_src[0];
$screen_src = wp_get_attachment_image_src( $custom_header->attachment_id, 'header_screen' );
$screen = $screen_src[0];
$screen_large_src = wp_get_attachment_image_src( $custom_header->attachment_id, 'header_screen_large' );
$screen_large = $screen_large_src[0];
if ( $mobile ) list($width_mobile, $height_mobile, $type_mobile, $attr_mobile) = getimagesize( $mobile );
if ( $tablet ) list($width_tablet, $height_tablet, $type_tablet, $attr_tablet) = getimagesize( $tablet );
if ( $screen ) list($width_screen, $height_screen, $type_screen, $attr_screen) = getimagesize( $screen );
if ( $screen_large ) list($width_screen_large, $height_screen_large, $type_screen_large, $attr_screen_large) = getimagesize( $screen_large );
$alt = strip_tags( get_post_meta( $custom_header->attachment_id, '_wp_attachment_image_alt', true ) );
}
?>
<picture itemprop="image">
<source media="(max-width: 479px)" srcset="<?php echo $mobile ?>" type="<?php echo image_type_to_mime_type ( $type_mobile ) ?>" <?php echo $attr_mobile ?>>
<source media="(max-width: 767px)" srcset="<?php echo $tablet ?>" type="<?php echo image_type_to_mime_type ( $type_tablet ) ?>" <?php echo $attr_tablet ?>>
<source media="(max-width: 991px)" srcset="<?php echo $screen ?>" type="<?php echo image_type_to_mime_type ( $type_screen ) ?>" <?php echo $attr_screen ?>>
<source media="(max-width: 2000px)" srcset="<?php echo $screen_large ?>" type="<?php echo image_type_to_mime_type ( $type_screen_large ) ?>" <?php echo $attr_screen_large ?>>
<img src="<?php header_image(); ?>" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="<?php echo $alt ?>" />
</picture>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment