Custom Post Types in loop

If you’re using my controller code, you’d just need to access those things in a way that allows you to access them directly instead of using functions that assume the availability of $post (as the_post_thumbnail() does). For WordPress content, many functions have a get_ variant that allows you to pass a post ID to specify the post you want to get data from. For instance, the_post_thumbnail() has get_the_post_thumbnail().

To use it, you could modify the code I posted to something similar to this following:

// controllers/FrontPage.php
public function galleryLoop()
{
    $gallery_items = get_posts([
        'post_type' => 'gallery',
        'posts_per_page'=>'10',
    ]);

    return array_map(function ($post) {
        return [
            'content' => apply_filters('the_content', $post->post_content),
            'thumbnail' => get_the_post_thumbnail($post->ID, 'large'),
        ];
    }, $gallery_items);
}

// views/front-page.blade.php
@foreach($gallery_loop as $gallery_item)
    {!! $gallery_item['content'] !!}
    {!! $gallery_item['thumbnail'] !!}
@endforeach

Custom fields you can access similarly, depending on what you’re using to create them. In general, they can be accessed with get_post_meta(). Some frameworks give you other functions to access the custom fields they create—i.e. ACF offers get_field().

10 Likes