I am trying to add code for featured image on different resolution. like thumbnail, medium, large and full. But I wanna look like this type of resolution.
e.g
125x125
150x150.
250x250
I am trying this code. but it's not work.
<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,array(300,300), true);?>
300x300
</br>
It's show full size of image.
You need to add different image sizes in your functions.php file. WordPress will then create these resized images when a new image is created.
Usage is as such:
<?php add_image_size( $name, $width, $height, $crop ); ?>
Lots more information available on the WordPress Codex.
If you wish to create these image sizes retrospectively (to images you've already added) then you'll need to regenerate your thumbnails. You can do this using the regenerate thumbnails plugin.
You can then reference a image size in your code. A full example:
Inside functions.php:
add_image_size( 'example_size', 200, 200, true );
Inside your theme:
$thumb_url = wp_get_attachment_image_src( $thumb_id, 'example_size', true );
Related
In WordPress admin, I have defined the size for medium as such:
I have then uploaded an image and defined in my code to use medium
$image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' );
However, the image size is being rendered at 370 x 186px. With the original size of the image being 1200 x 600. Seems like the width settings are being applied, but not the height?
WordPress is maintaining the aspect ratio for the crop. 1200x600 (which is a ratio of 2:1) is downsized to 370x185 (ratio 2:1), the latter of which is rounded up to 186.
If you look at the thumbnail version there's a checkbox to crop the image exactly, however WordPress doesn't do that for the default image sizes because it is an unexpected experience. If you do want it to crop, you can manually register your own size using add_image_size and pass true for the last parameter which is crop
add_action(
'after_setup_theme',
static function() {
add_image_size( 'custom-medium', 370, 325, true );
}
);
I am trying to resize an image coming from the Advanced Custom Fields repeater field with no success.
I have set up a basic non-repeater image field in the exact same way elsewhere on the page and it works perfectly.
So I have the image size set up in the functions.php:
add_image_size( 'bio-size', 400, 260, true );
Then in the template I have:
<?php while( have_rows('bio') ): the_row();
$bioImgObj = get_sub_field('bio_image');
$bioImgSize = 'bio-size';
$bioImgUrl = $bioImgObj['sizes'][$bioImgSize];
?>
<img width="400" height="260" src="<?php echo $bioImgUrl; ?>"/>
This gives me the image, but does not resize it in any way.
When using ACF, or general WordPress file uploads, modifying image sizes or other configs for files after upload wont affect already uploaded files because the files are processed on upload.
I wanna resize post thumbnail in Wordpress. So I use that function: get_the_post_thumbnail($onepost->ID, array(234, 156)); But I recive images with size 300x189. Why is this happens?
You best bet it to add your thumbnail size by using at add_image_size function, then call that new thumnail in your function:
In your functions.php:
add_image_size( 'custom-thumb', 234, 156 );
In your template file or wherever you're calling the thumbnail:
the_post_thumbnail( 'custom-thumb' );
Note that you will probably have to regenate your thumbnails after you do this as thumbnails are created when you upload the image, thus all images you uploaded won't have this custom thumbnail size. Here is a good plugin for that:
https://wordpress.org/plugins/regenerate-thumbnails/
I am trying to control my post thumbnail size on two different pages.
I am using
set_post_thumbnail_size( 875, 175 );
To control the size on my index page. And then when they open the post I want the single.php to have the full size image.
Use get_the_post_thumbnail, you would call this in your single.php template and specify the size you want.
You add an image size with add_image_size(); (documentation) and then call on the thumbnail size in your theme with the_post_thumbnail();. You can add as many image sizes as you would like.
So lets create two sizes with
add_image_size( 'home-thumb', 330, 175, true);
add_image_size( 'single-thumb', 875, 300, true );
This will add them and you can call them using using the_post_thumbnail('single-thumb'); since you want the single page to have the full image size you can just use the_post_thumbnail();.
Try this:
if(is_single()) {
set_post_thumbnail_size( 875, 175 );
} else {
set_post_thumbnail_size( other dimensions here );
}
I have set crop in functions.php to resize Images for Posts.
Codex from Wordpress: add_image_size, post_thumbnail
add_theme_support('post-thumbnails');
set_post_thumbnail_size(100, 100, true); //size of thumbs
add_image_size( 'post-thumb', 180, 180, true ); //(cropped)
Usage is like below
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'post-thumb' ); } ?>
But it will not resize the thumb and shows the original size. What am I doing wrong?
Wordpress will only resize images on upload, you can use this plugin to resize images on the fly, so the first time you call an image size that does not exists, this plugin will resize the image so the next times image size will exist.