Reputation: 831
So I'm trying to make this functions data available to each context there this, but I'm stuck with how to actually get the data itself now that its part of the context.
I have the following:
add_filter( 'timber_context', 'fancySquares_get_instagram_images' );
function fancySquares_get_instagram_images( $context ) {
$context['instaImages'] = [];
$api = wp_remote_request("testUrlHere");
$api = json_decode($api['body']);
for($i = 0; $i < 20; $i++)
{
$images[$i] = [];
$images[$i]['image'] = $api->data[$i]->images->standard_resolution->url;
$images[$i]['url'] = $api->data[$i]->link;
$images[$i]['likes'] = $api->data[$i]->likes->count;
}
return $context;
}
I am them trying to print out the result to make sure I'm doing it right, it returns an empty Array():
{{ instaImages|print_r}}
Any help would be appreciated. Thank you!
Upvotes: 1
Views: 1494
Reputation: 831
Here is the for the above question, hope this helps someone in the future.
You'll add the filter:
code sample:
add_filter( 'timber_context', 'fancySquares_show_instagram_results' );
function fancySquares_show_instagram_results( $context ) {
$context['fancySquaresInstagram'] = fancySquares_get_instagram();
return $context;
}
function fancySquares_get_instagram()
{
if(get_transient('instagram'))
{
return get_transient('instagram');
}
else
{
$api = wp_remote_request("instagram-api-url");
$api = json_decode($api['body']);
$images = [];
for($i = 0; $i < 20; $i++)
{
$images[$i] = [];
$images[$i]['image'] = $api->data[$i]->images->standard_resolution->url;
$images[$i]['url'] = $api->data[$i]->link;
$images[$i]['likes'] = $api->data[$i]->likes->count;
}
set_transient('instagram', $images, 60*60*24); // expires every day
return $images;
}
}
You will then output with:
{% for insta in fancySquaresInstagram %}
{{insta['url']}}
{% endfor %}
or could print the whole thing to get a better idea of whats inside:
{{ fancySquaresInstagram|print_r }}
Upvotes: 1