Вопрос

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?

Это было полезно?

Решение

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:

Лицензировано под: CC-BY-SA с атрибуция
Не связан с wordpress.stackexchange
scroll top