php PHP函数预处理WordPress卡的twig模板

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php PHP函数预处理WordPress卡的twig模板相关的知识,希望对你有一定的参考价值。

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;
}

以上是关于php PHP函数预处理WordPress卡的twig模板的主要内容,如果未能解决你的问题,请参考以下文章