首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过$wpdb使用get_the_post_thumbnail ->get_results

如何通过$wpdb使用get_the_post_thumbnail ->get_results
EN

Stack Overflow用户
提问于 2012-11-30 21:09:54
回答 1查看 3.3K关注 0票数 2

我正在使用get_results获取一个包含数据的数组。在这个数据中有post ID。当我尝试检索带有post ID的图像时,我没有检索到图像。有什么想法吗?

这是我的代码。

首先,我在functions.php中添加下一段代码

代码语言:javascript
复制
/** 
 * 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);
}

在我显示图像的文件中,我有下一个代码。

代码语言:javascript
复制
<?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来逐行检索数据。

代码语言:javascript
复制
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);

}

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-30 22:21:21

如果你有图片ID,你需要使用wp_get_attachment_image_src( $imageid,'itg_post‘)或者任何你想要的图片大小。

我也不明白,既然可以执行WordPress查询,为什么还要使用SQL查询。下面是如何使用WP_Query完成此操作:

代码语言:javascript
复制
$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();
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13645933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档