2012-11-30 1 views
2

get_results를 사용하여 데이터가있는 배열을 가져옵니다. 이 데이터에는 게시물 ID가 있습니다. 게시물 ID로 이미지를 검색하려고 할 때 이미지를 검색하지 않습니다. 어떤 생각?

이것은 내 코드입니다.

먼저 나는 다음 코드를 가지고있는 이미지를 표시 내 파일에서 functions.php

/** 
* Check to see if the function exists 
* It is always a good practice to avoid any version conflict 
*/ 

if(function_exists('add_theme_support')) 
{ 
    /** Exists! So add the post-thumbnail */ 
    add_theme_support('post-thumbnails'); 

    /** Now Set some image sizes */ 

    /** #1 for our featured content slider */ 
    add_image_size($name = 'itg_featured', $width = 500, $height = 300, $crop = true); 

    /** #2 for post thumbnail */ 
    add_image_size('itg_post', 250, 250, true); 

    /** #3 for widget thumbnail */ 
    add_image_size('itg_widget', 40, 40, true); 

    add_image_size('projects_single',160,160, true); 
    add_image_size('post',592,auto,true); 
    add_image_size('post-mini',152,auto,true); 
} 

에 다음 코드를 추가합니다.

<?php 
    global $post,$wpdb; 
    $table = $wpdb->prefix.'posts'; 
    $query = "SELECT * FROM $table WHERE post_type='startups' and post_status='publish' order by post_date desc limit 0,1; "; 
    $result = $wpdb->get_results($query); 
?> 

(이는 데이터를 완벽하게 검색합니다).

이제 데이터 행을 검색 할 때 foreach를 수행합니다.

foreach ($result as $key) { 

//retrieve post ID 
$idpost = $key->ID; 

//this line retrieve information to post_type 
$infostartup = get_post_meta($key->ID,array()); 

//retrieve Image ID 
$imageid = get_post($infostartup['startup_photo'][0]); 

//Now I'm trying display image of post 

echo get_the_post_thumbnail($idpost); 
//this not display the image 

also I try using image ID but not display image. 
echo get_the_post_thumbnail($imageid); 

} 

어떤 아이디어가 있습니까?

답변

6

이미지 ID가 있으면 wp_get_attachment_image_src ($ imageid, 'itg_post') 또는 원하는 이미지 크기를 사용해야합니다.

또한 워드 프레스 쿼리를 할 수있을 때 SQL 쿼리를 사용하는 이유를 알지 못합니다. WP_Query로 수행하는 방법은 다음과 같습니다.

$args = array(
    'post_type' => 'startups', 
    'posts_per_page' => -1 
); 
$loop = new WP_Query($args); 
if($loop->have_posts()): while($loop->have_posts()): $loop->the_post(); 
    global $post; 
    echo get_the_post_thumbnail($post->ID, 'image_size'); 
endwhile; endif; 
wp_reset_postdata(); 
+0

동의합니다. Wordpress 관련 데이터를 가져 오기 위해 Wordpress 내에서 SQL Query를 사용하는 경우 오류가 발생할 가능성이 있습니다. [Get The Post Thumbnail] (http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail)이 또 다른 가능한 대안입니다. – maiorano84

+0

어떻게하면 post_type = 'startups'를 호출하고 모든 행을 표시하는 Wordpress 쿼리를 할 수 있습니까? –

+0

[WP Query] (http://codex.wordpress.org/Class_Reference/WP_Query)를 사용하는 것이 가장 좋습니다. – maiorano84

관련 문제