Passing Data to Blade Template Partial in Custom WP_Query

Filters and controllers are based on the WordPress Template Hierarchy, which does not include list-item-person. More details.

Data can be passed directly to partials when they are included, i.e.:

@include('partials.list-item-person', $data)

The data passed should be an indexed array. The array keys will be converted into variables when the partial is included, i.e.:

// our-team.blade.php
@include('partials.list-item-person', array('people' => $people_query))

// list-item-person.blade.php
@foreach($people as $person)
   {{$person->post_title}}
   // etc...
@endforeach

Using argument-less post-related functions (i.e. the_title()) is a little more complicated in the partials loops, and I tend to avoid it: I don’t like using functions that take no arguments and assume a global value to do their thing. If you really want to, this github issue has a technique I found for making $post available.

3 Likes