2014-10-21 6 views
0

내 게시물에 3 개의 이미지가 첨부되어 있습니다. 3 개의 이미지 URL을 3 개의 개별 변수로 바꾸고 배경 이미지를 css에 사용하고 싶습니다. 지금까지 3 개의 URL을 표시 할 수 있었지만 변수를 배경 이미지로 지정할 수는 없습니다.Wordpress attachment urls을 변수로 사용

if ($attachments = get_children(array(
       'post_type' => 'attachment', 
       'post_mime_type' => 'image', 
       'numberposts' => -1, 
       'post_status' => null, 
       'post_parent' => $post->ID 
       ))); 

      foreach ($attachments as $attachment) { 

       $mynewarray = wp_get_attachment_image_src($attachment->ID, 'rig'); 
       $anotherarray = $mynewarray[0]; 
       echo $anotherarray ; 
      } 

위의 결과는 here입니다.

어떻게 분리하여 $backgroundimage1, $backgroundimage2 & $backgroundimage3과 같이 변수로 변환 할 수 있습니까? 그래서 나는 페이지에서 나중에 전화 할 수 있습니다.

<div class="backgound-image" style="background-image: url(<?php echo $backgroundimage1 ?>)"> 
+0

도움을 주신 덕분에 다음 번에 내 서식이 개선되었습니다. 감사합니다. – Stephen

답변

2

모든 첨부 파일을 새 배열에 추가 할 수 있습니다. 원하는 배열과 동일한 인덱스를 얻습니다.

if ($attachments = get_children(array(
       'post_type' => 'attachment', 
       'post_mime_type' => 'image', 
       'numberposts' => -1, 
       'post_status' => null, 
       'post_parent' => $post->ID 
       ))); 

      $mynewarray = array(); 
      foreach ($attachments as $attachment) { 

       $mynewarray = wp_get_attachment_image_src($attachment->ID, 'rig'); 
       $anotherarray[] = $mynewarray[0]; 
       // echo $anotherarray ; 
       // not echo, only define in here 
      } 

defination 후이 값을 얻을 수 있습니다.

echo $mynewarray[0]; // or [1] or [2] 
+0

와우, 좋아. 솔루션을 찾기 위해 8 시간을 검색합니다. 여기에 게시물을 작성하고 5 분 안에 문제를 해결하는 데 5 분이 걸렸습니다. 너무나 감사합니다. Fatih Sari – Stephen

+0

@Stephen 답을 수락하는 것을 잊지 마십시오. http://meta.stackoverflow.com/questions/251078/how-to-update-and-accept-answers –