How to deal with wordpress images?

I have blog post featured image that has specific size.

then I want to display it with its excerpt in different page called front-page.php, but I want to display it with larger size.

It already appear in the front page as I want but it is in its normal size.

I just want it to appear larger to be 458 x 355. I already put the dimensions on the code but it is not applied. here is the code

<?php echo get_the_post_thumbnail($post['ID'], array(458, 355)); ?>

please, what should I do?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Passing explicit dimensions to those APIs won’t give you an image with those dimensions unless one already exists. It will choose the image size that’s closest.

So, you have 2 options

  1. Add a named image size via add_image_size that matches the desired dimensions. WordPress will generate an image of this size on upload. You will need to use a regenerate thumbnails type plugin to apply this to past uploads
  2. Refactor your CSS to use object-fit: cover; then use a larger image size, then specify the width and height of the image explicitly ( which you should be doing anyway instead of relying on the image dimensions to determine the img elements width and height. The advantage of this is that images that are smaller, larger images, and images with different aspect ratios, will all work just fine.

e.g.

...yourfeaturedimg... {
    width: 458px;
    height: 355px;
    object-fit: cover;
}

I recommend option 2 as it’s more performant, easier to test, and involves following the HTML spec. It also makes your theme more robust and able to handle images better.

Further reading:


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x