Custom thumbnail size in controller

I have a controller that sets a number of post conditions. The partial loops through post type and view renders the post. So happy. One thing that I cant work out is passing custom image size that I’ve set up.

I’ve tried adding a parameters to get_post_thumbnail but the only thing that works is the following. Also tried
@thumbnail(‘my-custom-size’)

Any ideas would be much appreciated. Im sure it’s rather simple.

So far:

namespace App\Controllers;
use Sober\Controller\Controller;
class TemplateNews extends Controller
{
    public static function newsLoop($postType)
    {
        $item = get_posts([
            'post_type' => $postType,
            'posts_per_page' => 9,
        ]);

        return array_map(function ($post) {
            return [
                'content' => apply_filters('the_excerpt', $post->post_content),
                'title' => apply_filters('get_the_title', $post->post_title),
                'permalink' => apply_filters('permalink', get_permalink($post)),
                'thumbnail' => get_the_post_thumbnail_url($post)
            ];
        }, $item);
    }
}

Can you please describe, in detail, what you expect to happen and what is happening instead? There are many missing details here, including how this is failing to return the image size you want. Does it return no image? The image at thumbnail size (150x150)? Something else? What IS the image size you want? What is the size of the source image?

  • It returns an image for each post as part of the loop as expected.
  • The image size is the original dimensions I added the to featured image of the post.
  • I was hoping there was an extra parameter/argument I could add to the get_post_thumbnail_url to add my custom image size that I’ve added in functions.php. Eg. my-custom-size

My setup

TemplateNews.php

<?php
namespace App\Controllers;
use Sober\Controller\Controller;
class TemplateNews extends Controller
{
    public static function newsLoop($postType)
    {
        $item = get_posts([
            'post_type' => $postType,
            'posts_per_page' => 9,
        ]);

        return array_map(function ($post) {
            return [
                'content' => apply_filters('the_excerpt', $post->post_content),
                'title' => apply_filters('get_the_title', $post->post_title),
                'permalink' => apply_filters('permalink', get_permalink($post)),
                'thumbnail' => get_the_post_thumbnail_url($post)
            ];
        }, $item);
    }
}

template-news.blade.php

<div class="row">
    <div class="card-group">
      @foreach(TemplateNews::newsLoop('news') as $item)
      <div class="col-md-4 mb-4">
        @include('partials.content-news')
      </div>
      @endforeach
    </div>
  </div>
</div>
@php(wp_reset_postdata())

content-news.blade.php

  <div class="card h-100" @php(post_class())>
    <img class="card-img-top" src="{!! $item['thumbnail'] !!}" alt="">
    <div class="card-body">
      <h5 class="card-title">{!! $item['title'] !!}</h5>
      <p class="card-text">{!! $item['content'] !!}</p>
    </div>
    <div class="card-footer">
      <small class="text-muted"><a href="{!! $item['permalink'] !!}">Read article</a></small>
    </div>
  </div>

HTML render:

<div class="col-md-4 mb-4">
  <div class="card h-100">
    <img class="card-img-top" src="/wp-content/uploads/2020/06/news-hero-featured.jpg" alt="">
    <div class="card-body">
      <h5 class="card-title">4</h5>
      <p class="card-text">post content</p>
    </div>
    <div class="card-footer">
      <small class="text-muted"><a href="https://myblog.com/news-article-01">Read article</a></small>
    </div>
  </div>
</div>

Thanks!

get_the_post_thumbnail() does take a second argument that’s the name of an image size, i.e.

get_the_post_thumbnail($postID, 'large');

If that’s not returning the image size you expect, I don’t think that would have anything to do with your Controller. The issues I’d immediately think of are:

  • You’ve misspelled the name of your image size (probably not, but it’s easy to check)
  • The image in question is smaller than the image size you’re request, so WordPress just gives you the largest one it has; the original image
  • You didn’t wrap add_image_size() in an appropriate hook, so it isn’t being fired and making your image size available
  • You added the custom image size but haven’t regenerated thumbnails, so there is no image file of that size (WP only generates resized images on upload; it won’t generate new images on the fly)
1 Like

Thank you.

:grinning:

This topic was automatically closed after 42 days. New replies are no longer allowed.