繁体   English   中英

Wordpress:如何在$ wpdb-> get_results中使用get_the_post_thumbnail

[英]Wordpress: How to use get_the_post_thumbnail with $wpdb->get_results

我正在使用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);

}

任何想法?

如果您具有图像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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM