2012-11-30 41 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')或任何您想要的圖像大小。

我也不知道爲什麼你可以做一個WordPress查詢時使用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中使用SQL查詢來獲取與Wordpress相關的數據,那麼你可能會犯錯。 [獲取帖子縮略圖](http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail)是另一種可能的選擇。 – maiorano84

+0

我怎麼可以做一個Wordpress查詢,調用post_type ='startups'並顯示所有行? –

+0

使用[WP Query](http://codex.wordpress.org/Class_Reference/WP_Query)將是您最好的選擇。 – maiorano84

相關問題