WP introduced support for srcset in version 4.4 for Thumbnails and Post images. But I can't find a way to make the page header responsive. Here is how I embed the Page header:
<img src="<?php header_image() ?>" alt="">
This loads the header image (which can be uploaded in the backend > Design > Customise) in an src. But I'd rather include all custom image sizes (that I added in functions.php) of this image and put them in an srcset attribute. But there seems to be only one size for the header?
It won't be easy, but this is how you make Header images (banner) responsive with a srcset.
ps.: Please fix this, wordpress! Responsive Headers should be part of the srcset update.
Solution: We never use the WP header_image(); function, but instead only just use the custom header as an "Uploader" for our image that we then embed manually:
Extract the attachement ID of the Header image
Manually load src and srcset of this attachement ID
header.php
<?php
// Extract header attachement ID
$data = get_theme_mod('header_image_data');
$data = is_object($data) ? get_object_vars($data) : $data;
$header_id = is_array($data) && isset($data['attachment_id']) ? $data['attachment_id'] : false;
if($header_id) :
// Set image sources manually
$src = wp_get_attachment_image_src( $header_id, 'thumbnail-head' )[0];
$srcset = wp_get_attachment_image_srcset( $header_id, 'thumbnail-head' ); ?>
<img id="masthead-bg" src="<?php echo $src ?>" srcset="<?php echo $srcset ?>" sizes="100vw" alt="">
<?php endif; ?>
In this example thumbnail-head is my destination image size and aspect ratio. You can use whatever size you want (needs to have a fixed aspect ratio). We now have to add this image size to the functions.php file. In order to get larger sizes of this thumbnail (to embed in the srcset) you also have to add more sizes to the functions.php:
functions.php
add_image_size( 'thumbnail-head', 450, 300, true );
add_image_size( 'thumbnail-head-2x', 900, 600, true );
add_image_size( 'thumbnail-head-4x', 1800, 1200, true );
My thumbnail size is 450 x 300 pixel (3:2 aspect ratio). So I added a 2x and 4x version. Don't forget to rebuild thumbnails via plugin.
Finally it's important to set the dimensions of the custom header image to the largest version of your thumbnail. This is because WP cuts the image down to this size and creates the other sizes afterwords based on this cut down image. In this case search for your custom header in the functions.php and set width and height to 1800 and 1200. We also disable "flex-width" and "flex-height" to force the same aspect ratio as our added image sizes.
functions.php
function fs_custom_header_setup() {
add_theme_support( 'custom-header', apply_filters( 'fs_custom_header_args', array(
'default-image' => '',
'header-text' => false,
'default-text-color' => 'ffffff',
'width' => 1800,
'height' => 1200,
'flex-width' => false,
'flex-height' => false,
'wp-head-callback' => 'fs_header_style',
) ) );
}
add_action( 'after_setup_theme', 'fs_custom_header_setup' );
This is now possible using the_header_image_tag();.
You might want to use this, to avoid WP from hardcoding a height and width for responsive images:
the_header_image_tag( array(
'width' => "",
'height' => "",
));
Related
I'am trying to make custom thumbnail sizes in Wordpress. Currently I have following code in functions.php
<?php
add_image_size( 'featuredImageCropped', 310, 150, false );
function custom_excerpt_length( $length ) {
return 15;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
And I'am trying to access this thumbnail in index.php with following code:
<img class="keis-image" src="<?php $kuva = get_field('kuva');$image = wp_get_attachment_image_src( $kuva['id'], "featuredImageCropped"); echo $image[0] ?>"/>
However it will return full image instead of resized thumbnail, if I change featuredImageCropped to large or some other basic thumbnail size it will return it as it should.
How could I get my custom thumbnail to render as I'd like to?
According to add_image_size() in the Documentation:
Add this to your theme's functions.php :
add_action( 'after_setup_theme', 'mytheme_custom_thumbnail_size' );
function mytheme_custom_thumbnail_size(){
add_image_size( 'thumb-small', 200, 200, true ); // Hard crop to exact dimensions (crops sides or top and bottom)
add_image_size( 'thumb-medium', 520, 9999 ); // Crop to 520px width, unlimited height
add_image_size( 'thumb-large', 720, 340 ); // Soft proprtional crop to max 720px width, max 340px height
}
To display a featured image with your new size (in this case “thumb-small”) in a post, just add:
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'thumb-small' ); } ?>
If your theme does not support featured images, you need to add this to your functions.php as well, inside of your setup function.
// Enable featured image
add_theme_support( 'post-thumbnails' );
If you add new thumbnail sizes to a site which already has media uploaded, you’ll need to regenerate thumbnails once for the new sizes to show up using this plugin:
Regenerate Thumbnails
Thumbnail Sizes
The default image sizes of WordPress are “thumbnail”, “medium”, “large” and “full” (the size of the image you uploaded). These image sizes can be configured in the WordPress Administration Media panel under Settings > Media. This is how you can use these default sizes with the_post_thumbnail():
the_post_thumbnail(); // without parameter -> 'post-thumbnail'
the_post_thumbnail( 'thumbnail' ); // Thumbnail (default 150px x 150px max)
the_post_thumbnail( 'medium' ); // Medium resolution (default 300px x 300px max)
the_post_thumbnail( 'large' ); // Large resolution (default 640px x 640px max)
the_post_thumbnail( 'full' ); // Full resolution (original size uploaded)
the_post_thumbnail( array(100, 100) ); // Other resolutions
<img class="icona" src="<?php $src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID) , array(85, 85 ) , false, ''); echo $src[0]; ?>" />
I'm trying to get the "thumbnail" size src for the image. Instead with array(85, 85 ) I just get it with width and height tags set to 85px (but the image src can be 200000x200000).
In your functions.php add the wp function add_image_size('newimage', width, height, true)
http://codex.wordpress.org/Function_Reference/add_image_size
In your theme file:
get_the_post_thumbnail($post->ID, 'newimage') or the_post_thumbnail('newimage')
if you're images have already been added you'll have to regenerate your thumbnails. Use the plugin regenerate thumbnails to do this. If you haven't added the images yet than you should be good to go.
This is the website where I'm trying the featured image on. The site uses wordpress. As you can see, the Image uses a custom size, not the size I want it to use. Besides, it applies to itself the .post img { } characteristics. Here you have the codes I use.
(In PHP functions):
if ( function_exists( 'add_theme_support' ) ) add_theme_support( 'post-thumbnails' ); set_post_thumbnail_size(640,205);
And then, in HTML
<?php the_post_thumbnail();?>
I wrote down the image size I wanted in the PHP Functions code, but it's not working! What am I doing wrong?
You could just set the image size when your using it, in the html, like this:
<?php
if ( has_post_thumbnail() ) {
set_post_thumbnail_size( 640,205 );
echo get_the_post_thumbnail( );
}
?>
If you want to set custom values for the thumbnails in your functions.php file, you will have to do something like this:
function mytheme_custom_thumbnail_size(){
add_image_size( 'thumb-small', 200, 200, true ); // Hard crop to exact dimensions (crops sides or top and bottom)
add_image_size( 'thumb-medium', 520, 9999 ); // Crop to 520px width, unlimited height
add_image_size( 'thumb-large', 720, 340 ); // Soft proprtional crop to max 720px width, max 340px height
}
add_action( 'after_setup_theme', 'mytheme_custom_thumbnail_size' );
I'am trying to make custom thumbnail sizes in Wordpress. Currently I have following code in functions.php
<?php
add_image_size( 'featuredImageCropped', 310, 150, false );
function custom_excerpt_length( $length ) {
return 15;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
And I'am trying to access this thumbnail in index.php with following code:
<img class="keis-image" src="<?php $kuva = get_field('kuva');$image = wp_get_attachment_image_src( $kuva['id'], "featuredImageCropped"); echo $image[0] ?>"/>
However it will return full image instead of resized thumbnail, if I change featuredImageCropped to large or some other basic thumbnail size it will return it as it should.
How could I get my custom thumbnail to render as I'd like to?
According to add_image_size() in the Documentation:
Add this to your theme's functions.php :
add_action( 'after_setup_theme', 'mytheme_custom_thumbnail_size' );
function mytheme_custom_thumbnail_size(){
add_image_size( 'thumb-small', 200, 200, true ); // Hard crop to exact dimensions (crops sides or top and bottom)
add_image_size( 'thumb-medium', 520, 9999 ); // Crop to 520px width, unlimited height
add_image_size( 'thumb-large', 720, 340 ); // Soft proprtional crop to max 720px width, max 340px height
}
To display a featured image with your new size (in this case “thumb-small”) in a post, just add:
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'thumb-small' ); } ?>
If your theme does not support featured images, you need to add this to your functions.php as well, inside of your setup function.
// Enable featured image
add_theme_support( 'post-thumbnails' );
If you add new thumbnail sizes to a site which already has media uploaded, you’ll need to regenerate thumbnails once for the new sizes to show up using this plugin:
Regenerate Thumbnails
Thumbnail Sizes
The default image sizes of WordPress are “thumbnail”, “medium”, “large” and “full” (the size of the image you uploaded). These image sizes can be configured in the WordPress Administration Media panel under Settings > Media. This is how you can use these default sizes with the_post_thumbnail():
the_post_thumbnail(); // without parameter -> 'post-thumbnail'
the_post_thumbnail( 'thumbnail' ); // Thumbnail (default 150px x 150px max)
the_post_thumbnail( 'medium' ); // Medium resolution (default 300px x 300px max)
the_post_thumbnail( 'large' ); // Large resolution (default 640px x 640px max)
the_post_thumbnail( 'full' ); // Full resolution (original size uploaded)
the_post_thumbnail( array(100, 100) ); // Other resolutions
In my theme-functions.php file, I have
add_image_size('ad-medium', $width = 250, $height = 250, true );
This determines the size that Wordpress creates 'ad-medium' thumbnails. Any changes to this code determine what is shown on the website instantly.
So if I change the 250 to 100, the width will be reduced to 100px. Unfortunately, this has three problems:
This changes the dimensions of all future upload resizing for ad-medium, so that's out of the question.
The height is ignored - I can't warp it - it stays 1:1 aspect ratio no matter what.
I can't write 100%. The percentage sign is not understood.
In context, the code is:
<?php
}
// activate theme support items
if (function_exists('add_theme_support')) { // added in 2.9
// this theme uses post thumbnails
add_theme_support('post-thumbnails', array('post', 'page'));
set_post_thumbnail_size(100, 100); // normal post thumbnails
// add default posts and comments RSS feed links to head
add_theme_support( 'automatic-feed-links' );
}
// setup different image sizes
if ( function_exists( 'add_image_size' ) ) {
add_image_size('blog-thumbnail', 150, 150); // blog post thumbnail size, box crop mode
add_image_size('sidebar-thumbnail', 140, 140, true); // sidebar blog thumbnail size, box crop mode
// create special sizes for the ads
add_image_size('ad-thumb', 75, 75, true);
add_image_size('ad-small', 100, 100, true);
add_image_size('ad-medium', $width = 250, $height = 250, true );
//add_image_size('ad-large', 500, 500);
}
// Set the content width based on the theme's design and stylesheet.
// Used to set the width of images and content. Should be equal to the width the theme
// is designed for, generally via the style.css stylesheet.
if (!isset($content_width))
$content_width = 500;
// This theme supports native menu options, and uses wp_nav_menu() in one location for top navigation.
function appthemes_register_menus() {
register_nav_menus(array(
'primary' => __( 'Primary Navigation', 'appthemes' ),
'secondary' => __( 'Footer Navigation', 'appthemes' ),
'theme_dashboard' => __( 'Theme Dashboard', 'appthemes' )
));
}
add_action( 'init', 'appthemes_register_menus' );
//ajax header javascript builder for child categories AJAX dropdown builder
function cp_ajax_addnew_js_header() {
global $app_abbr;
$parentPosting = get_option($app_abbr.'_ad_parent_posting');
// Define custom JavaScript function
?>
How can I leave the thumbnail resizing feature alone and add another line of code that will resize it to 100% width 100% height? At the moment, the whole page is resizing except the stubborn thumbnails.
Please let me know what to do.
Thanks.
See: http://codex.wordpress.org/Post_Thumbnails: Post Thumbnails are given a class "wp-post-image". They also get a class depending on the size of the thumbnail being displayed You can style the output with these CSS selectors.
So you can add to your theme css file:
#content img.ad-medium {height:100%; Width:100%;}