2017-10-08 10 views
1

ACF를 사용하여 클라이언트가 홈 페이지에서 전체 화면 배경으로 사용할 이미지를 업로드 할 수있게합니다. 요일에 따라 이미지가 달라집니다. 내가 배경 이미지에 대한 사용자 지정 이미지 크기를 만들었고 요일에 서버를 쿼리하고 해당 이미지를 찾을 수있는 기능이 있습니다. 올바른 이미지 크기를 반환하는 함수를 가져 오는 데 문제가 있습니다. 여기ACF에서 배경 이미지로 srcset 사용

function my_custom_background() { 

$today = date('l'); 
$image = get_field (strtolower($today)); 
$size = 'auto'; // (thumbnail, medium, large, full or custom size) 
$backgroundImg = wp_get_attachment_image ($image, $size); 


?><style>body.page-id-5 {background-image: url('<?php echo $backgroundImg; ?>'); }</style><?php 
} 

add_action('wp_head', 'my_custom_background'); 

그리고 렌더링되는 것입니다 : 어떤 제안에 대한

background-image: url('<img width="1920" height="1258" src="http://dev.whydidibother.com/klwdp/jumpstart-full-screen-image-daily-change-site/wp-content/uploads/sites/6/2017/10/amazing-beautiful-breathtaking-clouds.jpg" class="attachment-auto size-auto" alt="" srcset="http://dev.whydidibother.com/klwdp/jumpstart-full-screen-image-daily-change-site/wp-content/uploads/sites/6/2017/10/amazing-beautiful-breathtaking-clouds.jpg 1920w, http://dev.whydidibother.com/klwdp/jumpstart-full-screen-image-daily-change-site/wp-content/uploads/sites/6/2017/10/amazing-beautiful-breathtaking-clouds-300x197.jpg 300w, http://dev.whydidibother.com/klwdp/jumpstart-full-screen-image-daily-change-site/wp-content/uploads/sites/6/2017/10/amazing-beautiful-breathtaking-clouds-768x503.jpg 768w, http://dev.whydidibother.com/klwdp/jumpstart-full-screen-image-daily-change-site/wp-content/uploads/sites/6/2017/10/amazing-beautiful-breathtaking-clouds-1024x671.jpg 1024w, http://dev.whydidibother.com/klwdp/jumpstart-full-screen-image-daily-change-site/wp-content/uploads/sites/6/2017/10/amazing-beautiful-breathtaking-clouds-1200x786.jpg 1200w, http://dev.whydidibother.com/klwdp/jumpstart-full-screen-image-daily-change-site/wp-content/uploads/sites/6/2017/10/amazing-beautiful-breathtaking-clouds-800x524.jpg 800w, http://dev.whydidibother.com/klwdp/jumpstart-full-screen-image-daily-change-site/wp-content/uploads/sites/6/2017/10/amazing-beautiful-breathtaking-clouds-500x328.jpg 500w" sizes="(max-width: 1920px) 100vw, 1920px" />'); 

감사

여기 내 기능입니다.

답변

0

Wordpress reference for wp_get_attachment_image을 보면이 함수가 html 요소를 반환한다는 것을 알 수 있습니다.

$image = get_field(strtolower($today)); 
echo $image['sizes']['large']; // thumbnail, medium, large, full or custom 

도 참조 ACF 문서 image field (섹션 "사용자 정의 디스플레이 (다음과 같이 나는 이미지 개체를 반환하기 위해 ACF 이미지 필드를 설정 (실제로 배열을 반환하는) 다음 적당한 크기를 얻을 추천 arrray) ").