I am using WordPress, Jetpack Photon, and Bootstrap 4. Here is my image tag that works but can’t be good practice....
<?php echo '<img class="card-img img-fluid" src=' . the_post_thumbnail(array(375,300)) . '>'; ?>
I am trying to request the featured image with the_post_thumbnail(). I can’t get Photon to supply a scaled image that fits the width of my Bootstrap card container. The provided image is either a bit too slim or it overflows my card width. How can I get a Photon image to fit perfectly/responsively (width-wise)?
the_post_thumbnail(‘medium').... results in too small of an image for the card container.
the_post_thumbnail('large').... results in too large of an image and over flows it's card container.
Using Bootstrap’s max-width: 100% doesn’t seem to fix this problem.
Instead of image tag, try:
<?php the_post_thumbnail( 'full', array( 'class'=>'card-img img-fluid' ) ); ?>
The above function actually generate <img class="card-img img-fluid" src="http://img-src">
Add the following code to your functions.php file to define new image size.
add_image_size( 'name-of-size', 375, 300, true );
to know more about add image size click here.
**in the above line of code the name-of-size can be any unique name to represent the particular image size. The concept is while you upload any image to WordPress media it will crop to all image size defined and stored in wp-uploads folder.
***Also note that you will need to regenerate thumbnails for those images which were previously uploaded, if you need them in the new size.
Add the following code to your file to display image
<?php the_post_thumbnail( 'name-of-size', array( 'class'=>'card-img img-fluid' ) ); ?>
Hope this is helpful, Thank you
Related
In functions.php i have,
function university_features() {
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_image_size('professorLandscape', 400, 260, true);
add_image_size('professorPortrait', 480, 650, true);
add_image_size('pageBanner', 1500, 350, true);
}
add_action('after_setup_theme', 'university_features');
In single-professor.php, I have:
<div class="one-third">
<?php the_post_thumbnail('professorPotrait'); ?>
</div>
But the image size doesn't change. But, if I put the default size of "thumbnail" or any of the others like this:
<div class="one-third">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
it works. What could be the issue? I'm working on localhost.
I have checked your code and it seems everything is working perfectly. I think what you missed that after setting the new image sizes you did not regenerate those images. You can try to upload a new image and check; make sure you have uploaded a large size more than 480px X 650px for checking purpose, because WordPress will crop a image if you upload larger than that. For existing images which you have already uploaded in the admin panel you can use a third party plugin like "Regenerate Thumbnails", I have checked with this plugin, you can try this if you want. Hope it will work.
Try to edit the default image sizes in the admin panel
And instead of the post_thumbnail custom class added .
use the default ones and edit them in the wordpress admin panel
The default values for wordpress images are
Thumbnail size (150 x 150 pixels)
Medium size (maximum 300 x 300 pixels)
Large size (maximum 1024 x 1024 pixels)
Full size (the original size of the uploaded image)
According to https://enginescout.com.au/wordpress-image-sizes/
<div class="one-third">
<?php the_post_thumbnail('medium'); ?>
</div>
Go to your WordPress dashboard and log in.
Select Media in the Settings menu. Change the width and height dimensions in Media Settings to fit your needs.
Confirm your changes by clicking Save Changes.
Checkout this example by visual composer https://visualcomposer.com/blog/wordpress-image-sizes-guide/#changing-wordpress-default-image-sizes
Another possible solution would be
You can add a mask for the posts themselves so the image itself will be locked to a certain width and height of the mask for example 100vh and 100vw
Checkout the example in Material Design bootstrap5
you can apply the same thing by utilizing their styling in your theme or recreate another like it https://mdbootstrap.com/docs/standard/content-styles/masks/
Code example
<div class="bg-image">
<img src="https://mdbcdn.b-cdn.net/img/new/standard/city/053.webp" class="w-100" />
<div class="mask" style="background-color: rgba(0, 0, 0, 0.6)"></div>
</div>
The image I uploaded on wordpress had a dimension of 250x357 and gets uploaded good at this page. But the same image is being cropped to 250x240 on this page. I don't know what to do, and I tried alot of things but the image just gets cropped.
Also the image on the homepage (second link) must be 300x250, just want the image to be resized to fit it without streching. So with white borders.
You must find articles loot. And find call to featured image function. Maybe it's use wp_get_attachment_image(). Than you need change wp_get_attachment_image( $image ,'cb-360-240'); to wp_get_attachment_image( $image ,'full');.
I am trying to add quality images for my wordpress blog, I am adding
it but, in homepage and, list page images are coming very low size
like 320*240. which are very zooming images to 750*540 width. But in
single article page Image coming nicely. Please help me in this
this is the link wethinkk.com
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
use your preferred one :)
It completely depends on the code in your wordpress theme on Homepage and Loop page.
There is several ways to change the ratios of the picture in wordpress like aq_resize() function. Check your code in 3 pages, see what is happening, compare them and use the one that you want in all other places (ex. The code in single page is suitable so use that code in other pages.)
I have a site where I list lots of thumbnails (photos) on the front page in a grid layout. Each image is 120px x 120px. What is the best way to keep them sharp on high resolution devices?
Should I make them 240px x 240px and then in the code resize them like:
<img src="file.jpg" width="120" hgith="120">
I guess jpg is better than png because of the files sizes?
yes you can use it like you mention. no matter you r using png or jpg.
you can also set thumbnail size from WordPress setting.
You can try following code
<?php
$size=array(240,240);
echo get_the_post_thumbnail( $post_id, $size, "attachment-thumbnail" );
?>
https://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
I've edited a Bootstrap Wordpress theme to display featured images randomly when refreshed on the front page. But now on every other line the images are displaying huge gaps instead of images -
Here is my site.
What have I done wrong, and how do I get rid of these gaps? I used this code in index.php to display the images randomly -
<?php /* Start the Loop */ ?>
*<?php query_posts($query_string . '&orderby=rand') ?>*
<?php while ( have_posts() ) : the_post(); ?>
The source of the problem is that the images aren't all the same height. In the row right at the top of the screen grab you've pasted, the image on the right isn't as tall as the other two. So the browser thinks there's space for content underneath it. It adds an image there, and tries to float it left. It stops when it bumps up against something - the image in the second column in the row above. Then it stops. And the next image gets put underneath it. It's just the way float works in CSS.
So you have two options. Either crop all your images (or their containers, say the articles) to the same height, or use a jQuery library like masonry to lay out your images.
See this answer for a related problem and more discussion.
Don't float in the .pbox css. Use display: inline-block instead and you're good.
See screenshot and updated css at bottom-right: https://www.dropbox.com/s/3qvmhvz5dwlnekb/Screenshot%202014-04-16%2022.14.42.png
In short the gaps come from floating images left that aren't equal heights. You have two options to fix this with out editing your current HTML markup.
Option 1
Add a new image size, add that image size to the wp_query and regenerate your thumbnails.
3 Steps:
1) Create a new image size by adding the following to functions.php
add_image_size( 'home-thumbnail', 400, 400, true);
home-thumbnail = size variable, keep it simple and short
400, 400 = height, width
true = hard crop, WP will crop the image from the center.
http://codex.wordpress.org/Function_Reference/add_image_size
2) Add the new image size to the wp query.
e.g. <?php
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'home-thumbnail' );
}
?>
3) Regenerate your thumbnails with this plugin: http://wordpress.org/plugins/regenerate-thumbnails/
That's it!
Options 2
Pre-crop your images before upload so they're the same size. I'd recommend Opt 1 as these steps are apart of any development workflow and will ultimately increate your development flexibility / options.