2014-02-08 2 views
0

다음을 사용하여 Wordpress Woocommerce 제품에 대한 데이터를 가져옵니다. json에서 제품 데이터를 출력합니다.Wordpress 이미지 URL이 작동하지 않습니다.

<?php 
$args = array('post_type' => 'product', 'posts_per_page' => 200, 'product_cat' => 'clothes'); 
     $loop = new WP_Query($args); 


    $send_array = array(); 
     while ($loop->have_posts()) : $loop->the_post(); 
     global $product; 

    $send_array[] = array(

     'id' => get_the_ID(), 
     'title' => get_the_title(), 
     'content' => get_the_content(), 
     'regular_price' => get_post_meta(get_the_ID(), '_regular_price', true), 
     'image' =>wp_get_attachment_image_src(), 
     'sale_price'=> get_post_meta(get_the_ID(), '_sale_price', true) 
    ); 

    endwhile; 

    wp_reset_query(); 
     ob_clean(); 
     echo json_encode($send_array); 
     exit(); 

    ?> 

이 코드는 올바르게 작동하고 데이터를 올바르게 출력합니다. 그러나 image이 작동하지 않는 것 같습니다.

각 제품에 대한 이미지 URL을 가져오고 싶습니다. 위 코드에서 wp_get_attachment_image_src()을 시도했지만 행운은 없습니다.

위의 코드를 사용하여 각 제품의 이미지 URL을 가져오고 값을 배열의 image 키에 배치하려면 어떻게해야합니까?

답변

1

문제는 핵심적으로 wp_get_attachment_image_src() 기능을 호출하지 않는 것입니다. get_post_thumbnail_id() 기능을 사용하여 얻을 수있는 원하는 첨부 파일의 ID가 필요합니다.

그러나 wp_get_attachment_image_src()은 첨부 파일의 이미지 속성 "url", "width""height"을 포함하는 배열을 반환합니다.

URL 만 반환하는 wp_get_attachment url() 함수를 사용하는 것이 좋습니다.

Finnaly,이 코드는 당신을 위해 잘 작동합니다 : 워드 프레스 사본에이 기능에 대한

$send_array[] = array(

    'id' => get_the_ID(), 
    'title' => get_the_title(), 
    'content' => get_the_content(), 
    'regular_price' => get_post_meta(get_the_ID(), '_regular_price', true), 
    'image' => wp_get_attachment_url(get_post_thumbnail_id(get_the_ID())), 
    'sale_price'=> get_post_meta(get_the_ID(), '_sale_price', true) 
); 

더 많은 정보는 :

http://codex.wordpress.org/Function_Reference/get_post_thumbnail_id

http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

http://codex.wordpress.org/Function_Reference/wp_get_attachment_url

+0

처럼 일 에이 매력. 또한 링크가 매우 도움이되었습니다. 매우 감사합니다. – Tester

관련 문제