megclaypool
12/3/2018 - 5:46 PM

PHP function to preprocess WordPress cards for twig template

[PHP function to preprocess WordPress cards for twig template]

Created by Jason, then I added some more image fields that apply to the ERI site. This will need to be adapted for the image fields specific to each site.

function parse_cardslider_data($items) {
	$out = [];
	foreach($items as $item) {

    if (!empty(get_post_thumbnail_id($item->ID))) {
      $image_id = get_post_thumbnail_id($item->ID);
    } elseif (!empty(get_field('hero_image', $item->ID))) {
      $image_id = get_field('hero_image', $item->ID)['ID'];
    } elseif (!empty(get_field('homepagehero__image', $item->ID))) {
      $image_id = get_field('homepagehero__image', $item->ID)['ID'];
    } else {
			// this is where we'd put a default image / icon in case the linked element doesn't have an image
    }

		$card = [
			'title' => $item->post_title,
			'image' => wp_get_attachment_image($image_id, '242x195', null, ['sizes' => '(min-width: 992px) 30vw, (min-width: 768px) 50vw, 90vw']),
			'label' => get_field('label', $item->ID),
			'link' => get_permalink($item->ID)
		];
		// gotta reset image_id or else items with no image use the image from the last item
		$image_id = [];
		$out[] = $card;
	}

	return $out;
}