Wordpress:从数据库获取精选图片网址

时间:2016-04-28 11:08:21

标签: wordpress

如何从数据库中获取特色图片的网址?我将在前端显示精选图像。

3 个答案:

答案 0 :(得分:1)

特色图片存储在

  

带有meta_key _thumbnail_id的wp_postmeta表

你可以通过

获得它
$Featured_image = $wpdb->get_results("
SELECT p.*
  FROM net_5_postmeta AS pm
 INNER JOIN net_5_posts AS p ON pm.meta_value=p.ID 
 WHERE pm.post_id = $da_id
   AND pm.meta_key = '_thumbnail_id' 
 ORDER BY p.post_date DESC 
 LIMIT 15
 ",'ARRAY_A'

SELECT * from {$wpdb->prefix}_posts 
WHERE ID in (
SELECT meta_value FROM {$wpdb->prefix}_postmeta 
WHERE meta_key = '_thumbnail_id'
AND post_id = ':ID'
);

用您的帖子ID替换ID

在WordPress中获取后缩略图网址

<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
echo $thumb_url[0];
?>

供参考:URL

答案 1 :(得分:0)

您可以尝试此代码

if ( have_posts() ) : while ( have_posts() ) : the_post(); 
           if ( has_post_thumbnail() ) {
        $feat_image_url = wp_get_attachment_url( get_post_thumbnail_id() );
               // use the $feat_image_url variable as you like
           }
           endwhile;
         endif;

希望这有帮助

保重和快乐编码

答案 2 :(得分:0)

请尝试这个

<?php $query = new WP_Query($args); ?>
<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>

<?php if (has_post_thumbnail()): ?>
    <a class="feature_image" href="<?php echo wp_get_attachment_url( get_post_thumbnail_id(get_the_ID())); ?>">
        <?php the_post_thumbnail('thumbnail'); ?>
    </a>
<?php endif; ?>